Skip to content

Instantly share code, notes, and snippets.

@bjdixon
Created August 21, 2015 15:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bjdixon/303571144203fe49c555 to your computer and use it in GitHub Desktop.
Save bjdixon/303571144203fe49c555 to your computer and use it in GitHub Desktop.
flatten an array es5
function flatten(a) {
return a.map(function(val) {
return (Array.isArray(val)) ? flatten(val) : val;
});
}
//example usage
var arr = [1, 2, [3, 4], 5, [6, [7, [8]], 9], 10];
var flat = flatten(arr); // [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment