Created
September 23, 2021 15:36
-
-
Save mateja176/d5717814a910f1295998cc84994958b6 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
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