Created
August 21, 2015 15:26
-
-
Save bjdixon/303571144203fe49c555 to your computer and use it in GitHub Desktop.
flatten an array es5
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 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