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"}]
1
  • 1
    Look into Array.flat and Array.flatMap Commented Nov 4, 2022 at 9:52

2 Answers 2

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);

Sign up to request clarification or add additional context in comments.

Comments

0

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

2 Comments

Using .flat(2) would be more suitable imo
@jsejcksn's answer is flat-out better ;) (see what I did there?)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.