Skip to content

Instantly share code, notes, and snippets.

@iRealNirmal
Created May 23, 2016 10:34
Show Gist options
  • Save iRealNirmal/5d11265f1f45a8134559e03496211ea9 to your computer and use it in GitHub Desktop.
Save iRealNirmal/5d11265f1f45a8134559e03496211ea9 to your computer and use it in GitHub Desktop.
Function that flattens an array of arbitrarily nested arrays of integers into a flat array of integers
function flattenArrayOfArrays(a, r){
if(!r){ r = []}
for(var i=0; i<a.length; i++){
if(a[i].constructor == Array){
console.log(r)
flattenArrayOfArrays(a[i], r);
}else{
r.push(a[i]);
}
}
return r;
}
flattenArrayOfArrays([[1,2,[3]],4])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment