Created
June 29, 2016 18:54
-
-
Save chirag64/de9767fd8961e35d4e591f22ba8363be to your computer and use it in GitHub Desktop.
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
// 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