0

I have an array of objects in this shape;


const objectsArray = [{ projects: [] }, { components: [] }];

I want to merge it into a single object like this;


const mergedObject={ projects: [], components: [] }

Is there a better way of doing it other than this:

Object.keys(objectArray).map(key=>objectArray[key]).reduce((old,item)=>(
          {...old,...item}
),{})
Rickhomes
  • 135
  • 1
  • 8

1 Answers1

0

This might be a bit simpler:

const objectsArray = [{ projects: [1,2,3] }, { components: [4,5,6] }];

let mergedObjects

objectsArray.forEach(obj => mergedObjects = { ...mergedObjects, ...obj})

console.log(mergedObjects)
Riza Khan
  • 2,712
  • 4
  • 18
  • 42