Skip to content

Instantly share code, notes, and snippets.

@gr8pathik
Created July 18, 2017 12:25
Show Gist options
  • Save gr8pathik/de9993a7c490956f0875e57bbcdb72df to your computer and use it in GitHub Desktop.
Save gr8pathik/de9993a7c490956f0875e57bbcdb72df to your computer and use it in GitHub Desktop.
Flatten Array - Javascript
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