-3

I have a couple of objects inside of an array :

array1 =[{name : "Cena", age : 44},{job : "actor", location : "USA"}]

is there a way for me to merge these two objects to get something like :

array2 =[{name : "Cena", age : 44, job : "actor", location : "USA"}]

I tried looping through the elements but it is not a good option if the object is a big one, I guess. Any good solution using typescript?

MenimE
  • 274
  • 6
  • 18

1 Answers1

1

You can use Array.prototype.reduce:

const array1 = [{ name: "Cena", age: 44 }, { job: "actor", location: "USA" }];
const array2 = [array1.reduce((acc, cur) => ({ ...acc, ...cur }))];
console.log(array2);
Hao Wu
  • 17,573
  • 6
  • 28
  • 60