Skip to content

Instantly share code, notes, and snippets.

@danactive
Created October 31, 2017 03:36
Show Gist options
  • Save danactive/c240f700225deada1e519530d4a5c690 to your computer and use it in GitHub Desktop.
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
// functional programming using recursion in ES2015 syntax
const flatten = (arr = []) => arr.reduce(
(prev, curr) => prev.concat(Array.isArray(curr) ? flatten(curr) : curr), []
);
@danactive
Copy link
Author

danactive commented Oct 31, 2017

Run sample
flatten([undefined, null, [true, false], [1, 2, [3, , , , , , , 4, 5], 6], 'seven'])
or
flatten()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment