Skip to content

Instantly share code, notes, and snippets.

@cgbrucezjy
Created July 21, 2017 20:49
Show Gist options
  • Save cgbrucezjy/1269c3cbd419c68d54db6e788800c096 to your computer and use it in GitHub Desktop.
Save cgbrucezjy/1269c3cbd419c68d54db6e788800c096 to your computer and use it in GitHub Desktop.
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