Skip to content

Instantly share code, notes, and snippets.

@mateja176
Created September 23, 2021 15:36
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 mateja176/d5717814a910f1295998cc84994958b6 to your computer and use it in GitHub Desktop.
Save mateja176/d5717814a910f1295998cc84994958b6 to your computer and use it in GitHub Desktop.
const groupByProp = <P extends string, O extends { [Key in P]: string }>(
objects: O[],
prop: P,
) => {
return objects.reduce((groups, object) => {
const key = object[prop];
return { ...groups, [key]: (groups[key] ?? []).concat(object) };
}, {} as Record<O[P], O[]>);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment