Skip to content

Instantly share code, notes, and snippets.

@yiyizym
Created Dec 26, 2018
Embed
What would you like to do?
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