Skip to content

Instantly share code, notes, and snippets.

@vijayjangid
Created June 4, 2019 13:12
Show Gist options
  • Save vijayjangid/b0c4fe20adb96220757887881a8c17fd to your computer and use it in GitHub Desktop.
Save vijayjangid/b0c4fe20adb96220757887881a8c17fd to your computer and use it in GitHub Desktop.
JS flatten array using ES6
_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