Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Flatten array JS
var flattenArray = function(arr) {
var results = [];
arr.forEach(function(item) {
if(Array.isArray(item)) {
results = results.concat(flattenArray(item));
} else {
results.push(item);
}
});
return results;
}
var myArr = [2, [ 3, 4, [ 5 ] ,[ 6, [ 7, [ 8 ]]], 5]];
console.log(flattenArray(myArr));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.