Skip to content

Instantly share code, notes, and snippets.

@bradoyler
Last active August 29, 2015 14:13
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 bradoyler/622cc78de41e4e0a9c4f to your computer and use it in GitHub Desktop.
Save bradoyler/622cc78de41e4e0a9c4f to your computer and use it in GitHub Desktop.
flatten a list / array in js
//input: [[1], 2, [[3,4], 5], [[[]]], [[[6]]], 7, 8, []]
function flatten(list) {
return list.reduce(function (acc, val) {
return acc.concat(val.constructor === Array ? flatten(val) : val);
}, []);
}
// output: [1, 2, 3, 4, 5, 6, 7, 8]
// more info: http://rosettacode.org/wiki/Flatten_a_list
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment