Skip to content

Instantly share code, notes, and snippets.

@miguelmota
Last active January 15, 2018 12:09
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save miguelmota/5f27d5cdb8462fbbb402 to your computer and use it in GitHub Desktop.
Save miguelmota/5f27d5cdb8462fbbb402 to your computer and use it in GitHub Desktop.
Flatten array recursively in JavaScript
function flatten(array) {
return Array.isArray(array) ? [].concat.apply([], array.map(flatten)) : array;
}
console.log(flatten('a')); // ["a"]
console.log(flatten([[['b']]])); // ["b"]
console.log(flatten(['a',['b'],['c']])); // ["a","b","c"]
console.log(flatten([['a'],'b',['c',['d']]])); // ["a","b","c","d"]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment