Created
July 21, 2017 20:49
-
-
Save cgbrucezjy/1269c3cbd419c68d54db6e788800c096 to your computer and use it in GitHub Desktop.
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 flattenArray(array){ | |
var newArray=[] | |
var stillHasNested=false; | |
array.forEach((item,i)=>{ | |
if(item instanceof Array) | |
{ | |
item.map(child=>{ | |
newArray.push(child) | |
if(child instanceof Array) | |
{ | |
stillHasNested=true | |
} | |
}) | |
} | |
else | |
newArray.push(item) | |
}) | |
if(stillHasNested) | |
flattenArray(newArray) | |
else | |
return newArray | |
} | |
console.log(flattenArray([1,[[3,[25,53,5],3],2,[12,12,32]]])) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment