Skip to content

Merge Array of objects JavaScript | Example code

  • by

Use Array.prototype.push.apply(arr1,arr2) or Spread Syntax to Merge Array of objects in JavaScript. Very simple using ES6 spread operator:

const array1 = [{a: 'HI!'}, {b: 'HOW'}]
const array2 = [{c: 'ARE'}, {d: 'YOU?'}]

const mergedArray = [ ...array1, ...array2 ]
console.log('Merged Array: ', mergedArray)

Example Merge Array of objects JavaScript

Simple HTML example code merges 2 arrays of objects in JavaScript.

<!DOCTYPE html>
<html>
<head>

  <script>

    var arr1 = [{name: "lang", value: "English"},{name: "age", value: "18"}];
    var arr2 = [{name : "childs", value: '5'}, {name: "lang", value: "German"}];

    Array.prototype.push.apply(arr1,arr2); 

    console.log(arr1);
  </script>

</head>
</html>

Output:

Merge Array of objects JavaScript

With ES6 Spread Syntax, you can do it very easy as below:

  <script>

    var arr1 = [{name: "lang", value: "English"},{name: "age", value: "18"}];
    var arr2 = [{name : "childs", value: '5'}, {name: "lang", value: "German"}];

    var arr3 = [...arr1, ...arr2];
    console.log(arr3)
  </script>

Do comment if you have any doubts or suggestions on this JS merge array topic.

Note: The All JS Examples codes are tested on the Firefox browser and the Chrome browser.

OS: Windows 10

Code: HTML 5 Version

Leave a Reply

Discover more from Tutorial

Subscribe now to keep reading and get access to the full archive.

Continue reading