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

unique-by.ts

10 lines380 Bytes

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