Skip to content

Instantly share code, notes, and snippets.

@rohit012
Created October 11, 2017 06:28
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 rohit012/5c008d3a7509293572be3d6ad543b122 to your computer and use it in GitHub Desktop.
Save rohit012/5c008d3a7509293572be3d6ad543b122 to your computer and use it in GitHub Desktop.
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