Skip to content
Source
Sign inSign up
TanStack/query
Open History to see the last commit

unique-by.ts

9 lines377 Bytes

1/**
2 * Removes duplicate items, keeping the first item for each key.
3 * @param arr - The items.
4 * @param fn - Returns the key that identifies an item.
5 * @returns The items with unique keys, in their original order.
6 */
7export function uniqueBy<T>(arr: Array<T>, fn: (x: T) => unknown): Array<T> {
8 return arr.filter((x, i, a) => a.findIndex((y) => fn(x) === fn(y)) === i)
9}