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 comparison.
OptionalkeyComparator: EqualityComparator<TKey, TKey>Optional equality comparator used to compare keys. Defaults to the library's standard equality comparison.
A sequence that yields the first element in each run of elements whose keys change.
Enumeration stops comparing elements once a different key is encountered, making this useful for collapsing grouped data.
const products = [
{ name: 'Apple', category: 'Fruit' },
{ name: 'Banana', category: 'Fruit' },
{ name: 'Carrot', category: 'Vegetable' },
{ name: 'Broccoli', category: 'Vegetable' },
{ name: 'Orange', category: 'Fruit' },
];
const distinctByCategory = distinctUntilChangedBy(products, p => p.category).toArray();
console.log(distinctByCategory);
// [
// { name: 'Apple', category: 'Fruit' },
// { name: 'Carrot', category: 'Vegetable' },
// { name: 'Orange', category: 'Fruit' }
// ]
Removes consecutive duplicate elements by comparing keys projected from each element.