Skip to content

Instantly share code, notes, and snippets.

@ben8p
Created May 31, 2016 12:57
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 ben8p/dc3060daec2801d1ea39d7d5be1fae5a to your computer and use it in GitHub Desktop.
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
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