Skip to content

Instantly share code, notes, and snippets.

@yuanoook
Created October 10, 2016 04:34
Show Gist options
  • Save yuanoook/78d7c710feb64dedcf129df2dd4b45a5 to your computer and use it in GitHub Desktop.
Save yuanoook/78d7c710feb64dedcf129df2dd4b45a5 to your computer and use it in GitHub Desktop.
Array flatten
/*
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