Skip to content

Instantly share code, notes, and snippets.

@yashikagarg13
Created February 28, 2017 16:49
Show Gist options
  • Save yashikagarg13/7013f8396d3b2c53e160e86b22618935 to your computer and use it in GitHub Desktop.
Save yashikagarg13/7013f8396d3b2c53e160e86b22618935 to your computer and use it in GitHub Desktop.
Flatten Arrays
function flatten(arr) {
return arr.reduce(function (acc, item) {
if (item instanceof Array) {
return acc.concat(flatten(item))
} else {
return acc.concat(item)
}
}, []);
}
const myArr = [[1, 2, 3], 4, [5, [6]], [[[[[[8]]]]]]];
console.log(flatten(myArr));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment