Created
August 30, 2013 00:54
-
-
Save amosrivera/6385209 to your computer and use it in GitHub Desktop.
Flatten array. No matter the depth.
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
var flatten = function(){ | |
var reduce = function (array) { | |
return array.reduce(function(r,e){ | |
return r.concat( (e instanceof Array) ? reduce(e) : e ); | |
},[]); | |
} | |
return reduce(Array.prototype.slice.call(arguments)); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
flatten(1, 2, 3, [4, 5], [[[6]]], [7, [8, [9]]],10); // returns [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
flatten(["a","b"],[1,2,3],[null,[1,2,4]]); // returns ["a", "b", 1, 2, 3, null, 1, 2, 4]