Created
December 15, 2018 13:46
-
-
Save veeracs/a11f0cb6a602823f37ae8d1cb844b96a to your computer and use it in GitHub Desktop.
Flatten Arrays using Reducer
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
const myarr = [1,2,[3]]; | |
function flatten(arr) { | |
if (Array.isArray(arr)) { | |
return arr.reduce((prev, next) => { | |
return prev.concat(flatten(next)); | |
}, []); | |
} else { | |
return arr; | |
} | |
} | |
console.log(flatten(myarr)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment