Home > OS >  Remove unnecessary arrays within arrays after map
Remove unnecessary arrays within arrays after map

Time:09-13

After a mapping an array 3 times i hva gotten this result:

[ [ [ [11], [22], [123], [asd] ] ] ]

How do i clean this array to become like so:


[ [11], [22], [123], [asd] ] 

cant remember how to do this or word this problem to get a answer. Thanks!!

CodePudding user response:

You can use Array.prototype.flat method to flatten your array.

If you know exactly how many levels you need to flatten, you can pass in that as an argument to the method.

In your case you can use arr.flat(2)

let arr = [ [ [ [11], [22], [123]] ] ];
console.log(arr.flat(2));

  • Related