Skip to content

Instantly share code, notes, and snippets.

@chirag64
Created June 29, 2016 18:54
Show Gist options
  • Save chirag64/de9767fd8961e35d4e591f22ba8363be to your computer and use it in GitHub Desktop.
Save chirag64/de9767fd8961e35d4e591f22ba8363be to your computer and use it in GitHub Desktop.
// Recursive function to flatten out a multi-dimensional array
function FlattenArray(arr) {
var newArr = [];
for (var i = 0; i < arr.length; i++) {
// Go through each elements of array, if element is array, call this function with it as parameter and then concatenate
// with the new array, else just add the element to the new array, then return the new array in the end
if (Array.isArray(arr[i])) {
newArr = newArr.concat(FlattenArray(arr[i]));
}
else {
newArr.push(arr[i]);
}
}
return newArr;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment