Created
March 2, 2019 21:21
-
-
Save keithcom/6595d536ddd5bbdfbe27455baa9f258c to your computer and use it in GitHub Desktop.
Flatten nested arrays
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
/* | |
* Flatten a multidimentional array of integers to a single dimention with the integer values | |
*/ | |
function flatten_array(arr) { | |
var newArr = [] | |
for (var idx=0; idx<arr.length; idx++) { | |
if (Array.isArray(arr[idx])) { | |
var a = flatten_array(arr[idx]); | |
for (var i=0; i<a.length; i++) { | |
newArr.push(a[i]); | |
} | |
} | |
else { | |
newArr.push(arr[idx]); | |
} | |
} | |
return newArr; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment