0

I'm working with an API and after I try to clean up the data, I got an array of arrays of arrays:

arr = [[[{name: "john"}],[{name: "jack"}]],[[{name: "joe"}],[{name: "bob"}]]]

How can I clean this up to something like this:

arr = [{name: "john"},{name: "jack"},{name: "joe"},{name: "bob"}]
Aidenhsy
  • 915
  • 2
  • 13
  • 28

2 Answers2

3

You can use Array.prototype.flat(), providing Infinity as the depth argument to flatten all sub-arrays recursively:

const arr = [[[{name: "john"}],[{name: "jack"}]],[[{name: "joe"}],[{name: "bob"}]]];

const flattened = arr.flat(Infinity);

console.log(flattened);
jsejcksn
  • 27,667
  • 4
  • 38
  • 62
0

In this case calling arr.flat().flat() would do the trick.

Some random IT boy
  • 7,569
  • 2
  • 21
  • 47