Skip to content

Instantly share code, notes, and snippets.

@amosrivera
Created August 30, 2013 00:54
Show Gist options
  • Save amosrivera/6385209 to your computer and use it in GitHub Desktop.
Save amosrivera/6385209 to your computer and use it in GitHub Desktop.
Flatten array. No matter the depth.
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));
}
@amosrivera
Copy link
Author

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]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment