Created
October 8, 2017 10:01
-
-
Save janumpallykalyan/af40097dc97452ce4c4d09b12d1234db to your computer and use it in GitHub Desktop.
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]; | |
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