Last active
December 12, 2016 18:21
-
-
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].
This file contains 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(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