0
const arr1 = [
[{ id: 1 }],
[{ id: 2 },{ id: 3 }], 
[{ id: 4 }]
];

I want to add all items in this array how can I do this. I want my output as Like:

const arr2 = [
     {id:1},
     {id:2},
     {id:3},
     {id:4}
   ]

1 Answers1

0

Using arr1.flat() will work for your example. If you have an array of even greater depth of nested arrays of objects, you can even use arr1.flat(Infinity) to flatten to a variable depth:

const arr1 = [
  { id: 0 },
  [{ id: 1 }],
  [{ id: 2 },{ id: 3 }], 
  [{ id: 4 }]
];

console.log(arr1.flat());
Brandon McConnell
  • 5,776
  • 1
  • 20
  • 36