2

I been searching for 3 hours and couldnt find an answer. The only think i can think of is to use for loop and push each element.

How i got this problem. I fetched some charges, each charge has an array of items. So I map the charge and returned the array items but It gave me this.

Example

let object = [
[{id:1},{id:2}],
[{id:3},{id:4}],
[{id:5},{id:6}]
]

What i am looking for :

object =[{id:1},{id:2},{id:3},{id:4},{id:5},{id:6}]
Mag Ron
  • 51
  • 5

2 Answers2

2

You can use Array#flat as follows:

const object = [
  [{id:1},{id:2}],
  [{id:3},{id:4}],
  [{id:5},{id:6}]
];

const res = object.flat();

console.log(res);

If object here is a result of mapping, Array#flatMap might be useful as well.

Majed Badawi
  • 27,616
  • 4
  • 25
  • 48
0

Simply, You can use concat to merge arrays:

let objects = [
[{id:1},{id:2}],
[{id:3},{id:4}],
[{id:5},{id:6}]
];
objects = [].concat.apply([], objects);

or use reduce

objects = objects.reduce(function(a, b){
     return a.concat(b);
}, []);

with arrow function ES2015

objects = objects.reduce((a, b) => a.concat(b), []);
XMehdi01
  • 5,538
  • 2
  • 10
  • 34