Skip to content

Instantly share code, notes, and snippets.

@keithcom
Created March 2, 2019 21:21
Show Gist options
  • Save keithcom/6595d536ddd5bbdfbe27455baa9f258c to your computer and use it in GitHub Desktop.
Save keithcom/6595d536ddd5bbdfbe27455baa9f258c to your computer and use it in GitHub Desktop.
Flatten nested arrays
/*
* 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