Created
October 11, 2023 16:18
-
-
Save dmshvetsov/c419a757137097134dc5e4cb598cb930 to your computer and use it in GitHub Desktop.
all sort every day utils for 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
function groupBy<T extends Record<string, string | number>, K extends keyof T>(list: T[], groupByKey: K): Record<T[K], T[]> { | |
return list.reduce((acc, item) => { | |
const groupValue = item[groupByKey] | |
if (!acc[groupValue]) { | |
acc[groupValue] = [item] | |
} else { | |
acc[groupValue].push(item) | |
} | |
return acc | |
}, {} as Record<T[K], T[]>) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment