Created
July 18, 2017 12:25
-
-
Save gr8pathik/de9993a7c490956f0875e57bbcdb72df to your computer and use it in GitHub Desktop.
Flatten Array - Javascript
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 flattenArray(array) { | |
var arrayPool = []; | |
if(array.length > 0) { | |
for(var i=0; i < array.length; i++){ | |
if(array[i].constructor === Array){ | |
arrayPool = arrayPool.concat(flattenArray(array[i])); | |
} else { | |
arrayPool.push(array[i]); | |
} | |
} | |
} | |
return arrayPool; | |
} | |
console.log(flattenArray([[1,2,[3]], 4])); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment