Created
February 28, 2017 16:49
-
-
Save yashikagarg13/7013f8396d3b2c53e160e86b22618935 to your computer and use it in GitHub Desktop.
Flatten Arrays
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) { | |
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