0

I work on a project with a lot of arrays and in some case I have this:

let main_array = [a,b,c,d,[e,f,g],h,i,j,[k,l,m,o],[p],q,[]]

In my main array I have a lot of subarrays, and I want to get all element from them without removing the other elements from the main array. So I want a function which can do this:

f(main_array) --> [a,b,c,d,e,f,g,h,i,j,k,l,m,o,p,q]

The particularity with the main array is about the number of arrays inside it, in fact, it could have 0 to n subarray(s) with 0 to n element(s).

Is there an existing built-in function or just a simple function with filter for example that can solve this problem in a few line of code (just 1 could be great !) ?

PS: I use node.js

3
  • Please visit the help center to see what and How to Ask. HINT: Post effort and CODE. Commented Dec 1, 2019 at 17:18
  • 3
    Array.prototype.flat Commented Dec 1, 2019 at 17:18
  • @ASDFGerte That's exactly what I want thanks ! Can you post it as answer please ? Commented Dec 1, 2019 at 17:21

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.