Skip to content

Instantly share code, notes, and snippets.

@kuldeepkeshwar
Last active December 12, 2016 18:21
Show Gist options
  • Save kuldeepkeshwar/002d97ba1eba74e931ee7ccefa5fe40d to your computer and use it in GitHub Desktop.
Save kuldeepkeshwar/002d97ba1eba74e931ee7ccefa5fe40d to your computer and use it in GitHub Desktop.
flatten an array of arbitrarily nested arrays of integers into a flat array of integers e.g. [[1,2,[3]],4] -> [1,2,3,4].
function flattenArray(arr){
return arr.reduce(function(initial,item){
if(typeof item=="number"){
initial.push(item);
}else{
initial=initial.concat(flattenArray(item))
}
return initial;
},[])
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment