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