Type of elements within the source iterable.
Key type returned by keySelector.
The source iterable.
Selector used to project each element to the key used for distinctness.
OptionalkeyComparator: EqualityComparator<TKey, TKey>Optional equality comparator used to compare keys. Defaults to the library's standard equality comparison.
A sequence that contains the first occurrence of each unique key.
Each element's key is evaluated exactly once; cache expensive key computations when possible.
const products = [
{ name: 'Apple', category: 'Fruit' },
{ name: 'Banana', category: 'Fruit' },
{ name: 'Carrot', category: 'Vegetable' },
];
const distinctByCategory = distinctBy(products, p => p.category).toArray();
console.log(distinctByCategory);
// [
// { name: 'Apple', category: 'Fruit' },
// { name: 'Carrot', category: 'Vegetable' }
// ]
Eliminates duplicate elements by comparing keys computed for each element.