Last active
March 16, 2021 22:40
-
-
Save maroun-baydoun/ec07608580113d132434698430e61db3 to your computer and use it in GitHub Desktop.
Flatten an array of arrays in TypeScript
This file contains hidden or 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 flatten = < T = any > (arr: T[]) => { | |
const reducer = < T = any > (prev: T[], curr: T | T[]) => { | |
if (curr.constructor !== Array) { | |
return [...prev, curr]; | |
} | |
return curr.reduce(reducer, prev); | |
}; | |
return arr.reduce(reducer, []); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment