Created
June 4, 2019 13:12
-
-
Save vijayjangid/b0c4fe20adb96220757887881a8c17fd to your computer and use it in GitHub Desktop.
JS flatten array using ES6
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
_flatten = input => | |
input.reduce((acc, curr) => | |
Array.isArray(curr) ? [...acc, ..._flatten(curr)] : [...acc, curr], []); | |
// test | |
_flatten([[1,2],3,4,[5,[6,7],8],[9,[10,[11]]]]); // [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment