Skip to content

Instantly share code, notes, and snippets.

@optimistanoop
Created July 20, 2016 09:03
Show Gist options
  • Save optimistanoop/a5051493fe0192da5ce907205e11d45b to your computer and use it in GitHub Desktop.
Save optimistanoop/a5051493fe0192da5ce907205e11d45b to your computer and use it in GitHub Desktop.
Array flatter using recursion.
function flatter(arr , res){
if(!Array.isArray(arr)){
return;
}
for(var elem in arr){
if(Array.isArray(arr[elem])){
flatter(arr[elem], res);
}else {
res.push(arr[elem]);
}
}
return res;
}
var res = flatter([1,[2,3,[4,5,6,[7,8]],9],10], []);
console.log(res);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment