Skip to content

Instantly share code, notes, and snippets.

@yiyizym
Created December 26, 2018 03:01
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 yiyizym/0a9b57383bdf5b64d125153c5e533f9e to your computer and use it in GitHub Desktop.
Save yiyizym/0a9b57383bdf5b64d125153c5e533f9e to your computer and use it in GitHub Desktop.
flatten array
function flatten(original){
let result = [], temp, child;
if(Array.isArray(original)){
temp = original.slice(0)
} else {
temp = [original]
}
while(temp.length){
if((child = temp.pop()) && child.pop){
for(let index = 0; index < child.length; index++){
temp[temp.length] = child[index]
}
} else {
result.unshift(child)
}
}
return result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment