Skip to content

Instantly share code, notes, and snippets.

@ferontv
Created February 22, 2019 02:57
Show Gist options
  • Save ferontv/b7c4670924997e2d38f5e02e93c6ef0d to your computer and use it in GitHub Desktop.
Save ferontv/b7c4670924997e2d38f5e02e93c6ef0d to your computer and use it in GitHub Desktop.
Exercise to flatten arrays from nested arrays
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