Last active
November 11, 2018 12:28
-
-
Save sachingvit/407cf25d0e986b85f2d679edb9e85783 to your computer and use it in GitHub Desktop.
JS - Flattening an array
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Flattening an array in JS. | |
Ex: Input : [[1, 2, 3], 4, [5, [6, 7]] | |
Output: [1, 2, 3, 4, 5, 6, 7] | |
::: Using concat() ::: | |
let list = [[1, 2, 3], 4, [5, [6, 7]]; | |
let flat_array= []; | |
list.forEach(data=>{ | |
flat_array = [...flat_array].concat(data) | |
}) | |
console.log(flat_array) | |
>>>> [1, 2, 3, 4, 5, 6, 7] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment