Skip to content

Instantly share code, notes, and snippets.

@jeankueo
Last active May 30, 2016 09:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jeankueo/58a8a9152cc1a5cc083565220eafc8e9 to your computer and use it in GitHub Desktop.
Save jeankueo/58a8a9152cc1a5cc083565220eafc8e9 to your computer and use it in GitHub Desktop.
flatten an unknown depth array
function flatArray (source) {
if (Array.isArray(source)) {
return source.reduce(function (previous, current) {
return previous.concat(flatArray(current));
}, [])
}
return [source];
}
@jeankueo
Copy link
Author

jeankueo commented May 30, 2016

This practice helps developers to understand array methods. Especially some new ones in ES5.

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