Skip to content

uniqBy

uniqBy<T, U>(iterable, keyOf, comparisonStrategy?): IterableIterator<T>

Defined in: set/uniq-by.ts:30

Yields the first item seen for each distinct keyOf(item), in first-occurrence order.

Time complexity: O(n) under the default "sameValueZero".

T

U

Iterable<T>

The input iterable.

Callable<[T], U>

A function that takes an item and returns a key.

EqualityComparisonStrategy<U, U> = "sameValueZero"

The equality comparison strategy for keys.

IterableIterator<T>

An iterator over the first item seen for each distinct key.

const array = [
{ id: 1, name: "Alice" },
{ id: 2, name: "Bob" },
{ id: 1, name: "Alice" },
];
[...uniqBy(array, (item) => item.id)];
// [{ id: 1, name: "Alice" }, { id: 2, name: "Bob" }]
```;