Skip to content

Instantly share code, notes, and snippets.

@ile
Created May 25, 2016 03:18
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 ile/3652fe63b769c3d5838d76d533a32a17 to your computer and use it in GitHub Desktop.
Save ile/3652fe63b769c3d5838d76d533a32a17 to your computer and use it in GitHub Desktop.
function flatten(arr) {
var arr2 = [];
function getValue(val) {
if (Array.isArray(val)) {
val.forEach(getValue)
}
else {
arr2.push(val);
}
}
arr.forEach(getValue);
return arr2;
}
flatten([[1,2,[3]],4]); // [1, 2, 3, 4]
flatten([[1,2,[3, 4, [5, 6]]], 7, [8]]); // [1, 2, 3, 4, 5, 6, 7, 8]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment