Last active
May 7, 2025 12:08
-
-
Save youyoumu/7e628a43c1dc1d242a1b91e6b974e670 to your computer and use it in GitHub Desktop.
Generic function for filtering unique items by a key
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
// Generic function for filtering unique items by a key | |
export function uniqueBy<T, K>(array: T[], keySelector: (item: T) => K): T[] { | |
return array.filter( | |
(item, index, self) => | |
index === self.findIndex((i) => keySelector(i) === keySelector(item)) | |
); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment