Created
April 13, 2021 09:57
-
-
Save fronterior/f72fd8785fe945a83a0e1571a666ddb0 to your computer and use it in GitHub Desktop.
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 groupBy = <T, K extends keyof any>(f: (p: T) => K, arr: T[]) => arr.reduce((acc, val) => { | |
const key = f(val); | |
acc[key]?.push(val) ?? (acc[key] = [val]); | |
return acc; | |
}, {} as Record<K, T[]>); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment