Created
May 23, 2016 10:34
-
-
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
This file contains hidden or 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 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