Skip to content

Instantly share code, notes, and snippets.

@optimistanoop
Created July 20, 2016 09:10
Show Gist options
  • Save optimistanoop/c19d70a801ff59c879ac164abd3ab35e to your computer and use it in GitHub Desktop.
Save optimistanoop/c19d70a801ff59c879ac164abd3ab35e to your computer and use it in GitHub Desktop.
Array flatter without recursion.
function flatter (arr) {
var clonedList = arr.slice(0)
var flatList = []
while (clonedList.length) {
var item = clonedList.shift()
if (item instanceof Array === true) {
clonedList = item.concat(clonedList)
} else {
flatList.push(item)
}
}
return flatList
}
var res = flatter([1,[2,3,[4,5,6,[7,8]],9],10]);
console.log(res);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment