Created
February 22, 2019 02:57
-
-
Save ferontv/b7c4670924997e2d38f5e02e93c6ef0d to your computer and use it in GitHub Desktop.
Exercise to flatten arrays from nested arrays
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
const flatArray = (nestArr, outRng, aux) => { | |
if(!aux){ aux = []} //aux empty | |
if (outRng) { | |
return aux.concat(...nestArr); //spread op | |
} | |
for(let i=0; i<nestArr.length; i++){ | |
if(nestArr[i].constructor == Array){ | |
flatArray(nestArr[i],outRng,aux); //recursive | |
}else{ | |
aux.push(nestArr[i]); | |
} | |
} | |
return aux; //return value | |
}; | |
console.log(flatArray([[1,2,[3]],4])); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment