Created
September 13, 2017 13:25
-
-
Save ashish173/7fe96d6797357edbcadffcb55bd481e1 to your computer and use it in GitHub Desktop.
Flatten an arran in 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
function flatten(arr) { | |
var i = 0; | |
while (i < arr.length) { | |
arr = arr.reduce(function (prev, curr) { | |
return prev.concat(curr); | |
}, []); | |
i++; | |
} | |
return arr; | |
} | |
flatten([1, [2], [3, [[4]]]]); // [1,2,3,4] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment