Created
October 31, 2017 03:36
-
-
Save danactive/c240f700225deada1e519530d4a5c690 to your computer and use it in GitHub Desktop.
will flatten an array of arbitrarily nested arrays of primitives into a flat array of primitives
This file contains 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
// functional programming using recursion in ES2015 syntax | |
const flatten = (arr = []) => arr.reduce( | |
(prev, curr) => prev.concat(Array.isArray(curr) ? flatten(curr) : curr), [] | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Run sample
flatten([undefined, null, [true, false], [1, 2, [3, , , , , , , 4, 5], 6], 'seven'])
or
flatten()