Created
May 31, 2016 12:57
-
-
Save ben8p/dc3060daec2801d1ea39d7d5be1fae5a to your computer and use it in GitHub Desktop.
flatten an array of arbitrarily nested arrays of integers into a flat array of integers
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
var data = [[1,2,[3,4,[5,6]]],7]; | |
function flatten(array) { | |
var output = []; | |
array.forEach(function(value) { | |
if(value instanceof Array) { | |
output = output.concat(flatten(value)); | |
} else { | |
output.push(value) | |
} | |
}); | |
return output; | |
} | |
console.log(flatten(data)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment