Last active
September 19, 2016 11:40
-
-
Save hilkbahar/bf1dfe6cb4ba3f42a0ce719777d00b42 to your computer and use it in GitHub Desktop.
Nested Integer Array to Flat Integer Array
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
function nestedToFlatArray(arr, returnArr) { | |
if(returnArr.length == 0) | |
returnArr = []; | |
for(var i=0; i<arr.length; i++) { | |
if(Array.isArray(arr[i])) { | |
nestedToFlatArray(arr[i], returnArr); | |
} | |
else { | |
returnArr.push(arr[i]); | |
} | |
} | |
return returnArr; | |
} | |
var mainArray = [[1,2,[3]],4]; | |
console.log(nestedToFlatArray(mainArray)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment