Skip to content

Instantly share code, notes, and snippets.

@hasangilak
Last active January 5, 2017 19:58
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 hasangilak/2b1739cf87434c5e939c045aef03a55e to your computer and use it in GitHub Desktop.
Save hasangilak/2b1739cf87434c5e939c045aef03a55e to your computer and use it in GitHub Desktop.
var crazyArray = [[1,2,[3]],4];
function flatten(crazyArray, items = []) {
var item;
while(crazyArray.length != 0){
item = crazyArray.pop();
if(Array.isArray(item)) {
flatten(item, items);
} else {
items.push(item);
}
}
return [...items].reverse();
}
console.log(flatten(crazyArray));
console.log(flatten([[1],[2],[3],[4],[5,6]]))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment