[Solved] Merge two array of objects into single array of objects in JavaScript? [closed]


If you have just two arrays, you could use the object spread operator to do something like the following:

function combine(arr1, arr2) {
  return arr1.map((obj, idx) => ({
    ...obj,
    ...arr2[idx]
  }));
}

var arr1 = [
  {
    name: "abc"
  },
   {
    name: "xyz"
  }
];

var arr2 = [
  {
    age: 18
  },
   {
    age: 21
  }
];

console.log(combine(arr1, arr2));

If you’re using lodash, you can easily do this by using _.merge:

var arr1 = [
  {
    name: "abc"
  },
   {
    name: "xyz"
  }
];

var arr2 = [
  {
    age: 18
  },
   {
    age: 21
  }
];
 
console.log(_.merge(arr1, arr2));
<script src="https://unpkg.com/[email protected]/lodash.min.js"></script>

solved Merge two array of objects into single array of objects in JavaScript? [closed]