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}
),{})
1
  • 1
    mergedObject = { ...objectsArray[0], ...objectsArray[1] } Commented Sep 17, 2021 at 11:34

1 Answer 1

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)
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.