Last active
August 30, 2018 21:08
-
-
Save NinoScript/25899e4dd0827cfb2b0fffb4e6c7bb38 to your computer and use it in GitHub Desktop.
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
export function groupBy<T, K>(array: T[], getKey: (value: T) => K): Map<K, T[]> { | |
return array.reduce( | |
(groups, item) => { | |
const key = getKey(item); | |
const innerArray = groups.get(key); | |
if (innerArray === undefined) { | |
groups.set(key, [item]) | |
} else { | |
innerArray.push(item) | |
} | |
return groups | |
}, | |
new Map<K, T[]>() | |
) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment