Skip to content

Instantly share code, notes, and snippets.

@dmshvetsov
Created October 11, 2023 16:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dmshvetsov/c419a757137097134dc5e4cb598cb930 to your computer and use it in GitHub Desktop.
Save dmshvetsov/c419a757137097134dc5e4cb598cb930 to your computer and use it in GitHub Desktop.
all sort every day utils for TypeScript
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