Created
January 4, 2019 19:35
-
-
Save raviteja83/382b8cc4bb5232b8f1c79897313d320d to your computer and use it in GitHub Desktop.
Flatten nested js arrays
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
const arr = [[1,2,[3]],4]; | |
const flatten = list => list.reduce( | |
(result, val) => result.concat(Array.isArray(val) ? flatten(val) : val), [] | |
); | |
console.log(flatten(arr)); //[1, 2, 3, 4] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment