Skip to content

Instantly share code, notes, and snippets.

@huttj
Created May 13, 2015 22:13
Show Gist options
  • Save huttj/4a25c0f45534964e482a to your computer and use it in GitHub Desktop.
Save huttj/4a25c0f45534964e482a to your computer and use it in GitHub Desktop.
Flatten nested arrays
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