How to flatten array in JavaScript

3 Answers

0 votes
const arr = [[1,2], [3,4], [5,6], [7], [8,9,10]];
const arr_flat = arr.flat();

for (let i = 0; i < arr_flat.length; i++)    
    console.log(arr_flat[i]);



/*
run:

1
2
3
4
5
6
7
8
9
10

*/

 



answered May 17, 2021 by avibootz
0 votes
const arr = [[1,2], [3,[4,5],6], [7], [8,9,10]];
const arr_flat = arr.flat();

for (let i = 0; i < arr_flat.length; i++)    
    console.log(arr_flat[i]);



/*
run:

1
2
3
(2)[4, 5]
6
7
8
9
10

*/

 



answered May 17, 2021 by avibootz
0 votes
const arr = [[1,2], [3,[4,5],6], [7], [8,9,10]];
const arr_flat = arr.flat(2);

for (let i = 0; i < arr_flat.length; i++)    
    console.log(arr_flat[i]);



/*
run:

1
2
3
4
5
6
7
8
9
10

*/

 



answered May 17, 2021 by avibootz

Related questions

...