Skip to content

Instantly share code, notes, and snippets.

@waleedsamy
Created April 10, 2017 21:55
Show Gist options
  • Save waleedsamy/79dd535e0f51dd034625ebfe098ce197 to your computer and use it in GitHub Desktop.
Save waleedsamy/79dd535e0f51dd034625ebfe098ce197 to your computer and use it in GitHub Desktop.
flatten [1, [2], 3, [4, 5, [6, 7, [8, 9, 10]]]] => [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 ]
const flatten = arr => arr.reduce(
(acc, val) => acc.concat(Array.isArray(val) ? flatten(val) : val), []
);
module.exports = {
flatten: flatten
}
// let list = [1, [2], 3, [4, 5, [6, 7, [8, 9, 10]]]]
// console.log(flatten(list));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment