Created
May 13, 2015 22:13
-
-
Save huttj/4a25c0f45534964e482a to your computer and use it in GitHub Desktop.
Flatten nested arrays
This file contains hidden or 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(arry) { | |
var result = []; | |
for (var i = 0; i < arry.length; i++) { | |
if (typeof arry[i] === 'number') { | |
result.push(arry[i]); | |
} else { | |
result = result.concat(flatten(arry[i])); | |
} | |
} | |
return result; | |
} | |
// Test | |
flatten([1,2,3,[4,5,6],[7,8,9,[10,11,12], 13], 14, 15]); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment