Skip to content

Instantly share code, notes, and snippets.

@ashish173
Created September 13, 2017 13:25
Show Gist options
  • Save ashish173/7fe96d6797357edbcadffcb55bd481e1 to your computer and use it in GitHub Desktop.
Save ashish173/7fe96d6797357edbcadffcb55bd481e1 to your computer and use it in GitHub Desktop.
Flatten an arran in JS
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