Created
October 10, 2016 04:34
-
-
Save yuanoook/78d7c710feb64dedcf129df2dd4b45a5 to your computer and use it in GitHub Desktop.
Array flatten
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
/* | |
Flatten an array of arbitrarily nested arrays of integers into a flat array of integers | |
[[1,2,[3]],4] -> [1,2,3,4] | |
<object Array> arrayFlatten(<object Array>); | |
*/ | |
function arrayFlatten ( ary, ret ) { | |
ret = ret === undefined ? [] : ret; | |
for (var i = 0; i < ary.length; i++) { | |
if (Array.isArray(ary[i])) { | |
arrayFlatten(ary[i], ret); | |
} else { | |
ret.push(ary[i]); | |
} | |
} | |
return ret; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment