Skip to content

Instantly share code, notes, and snippets.

@janumpallykalyan
Created October 8, 2017 10:01
Show Gist options
  • Save janumpallykalyan/af40097dc97452ce4c4d09b12d1234db to your computer and use it in GitHub Desktop.
Save janumpallykalyan/af40097dc97452ce4c4d09b12d1234db to your computer and use it in GitHub Desktop.
nested arrays of integers into a flat array of integers
var data = [[1,2,[3]],4];
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