Skip to content

Instantly share code, notes, and snippets.

@srkama
Created March 14, 2016 04:38
Show Gist options
  • Select an option

  • Save srkama/56d3d0f6a8196e657a18 to your computer and use it in GitHub Desktop.

Select an option

Save srkama/56d3d0f6a8196e657a18 to your computer and use it in GitHub Desktop.
flattening the array
function steamroller(arr) {
// I'm a steamroller, baby
return flatten(arr);
}
function flatten(a, r){
if(!r){ r = [];}
for(var i=0; i<a.length; i++){
if(Array.isArray(a[i])){
flatten(a[i], r);
}else{
r.push(a[i]);
}
}
return r;
}
steamroller([1, [2], [3, [[4]]]]);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment