Skip to content

Instantly share code, notes, and snippets.

@youyoumu
Last active May 7, 2025 12:08
Show Gist options
  • Save youyoumu/7e628a43c1dc1d242a1b91e6b974e670 to your computer and use it in GitHub Desktop.
Save youyoumu/7e628a43c1dc1d242a1b91e6b974e670 to your computer and use it in GitHub Desktop.
Generic function for filtering unique items by a key
// 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