Created
December 26, 2018 03:01
-
-
Save yiyizym/0a9b57383bdf5b64d125153c5e533f9e to your computer and use it in GitHub Desktop.
flatten array
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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