Skip to content

Instantly share code, notes, and snippets.

@eugene-kim
Created June 19, 2018 17:11
Show Gist options
  • Save eugene-kim/ef6192e1aeab90fc235596c0e2585532 to your computer and use it in GitHub Desktop.
Save eugene-kim/ef6192e1aeab90fc235596c0e2585532 to your computer and use it in GitHub Desktop.
Flatten Array
/**
* Return a flattened array when given an array of arbitrary depth.
*/
const flattenArray = array => array.reduce((accum, element) => {
if (Array.isArray(element)) {
const flattenedArray = flattenArray(element);
accum.push.apply(accum, flattenedArray);
} else {
accum.push(element);
}
return accum;
}, []);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment