Skip to content

Instantly share code, notes, and snippets.

@fronterior
Created April 13, 2021 09:57
Show Gist options
  • Save fronterior/f72fd8785fe945a83a0e1571a666ddb0 to your computer and use it in GitHub Desktop.
Save fronterior/f72fd8785fe945a83a0e1571a666ddb0 to your computer and use it in GitHub Desktop.
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