distinctUntilChanged
signature: distinctUntilChanged(compare: function): Observable
distinctUntilChanged(compare: function): ObservableOnly emit when the current value is different than the last.
๐ก distinctUntilChanged uses === comparison by default, object references must match!
๐ก If you want to compare based on an object property, you can use distinctUntilKeyChanged instead!
Examples
Example 1: distinctUntilChanged with basic values
( StackBlitz )
// RxJS v6+
import { from } from 'rxjs';
import { distinctUntilChanged } from 'rxjs/operators';
// only output distinct values, based on the last emitted value
const source$ = from([1, 1, 2, 2, 3, 3]);
source$
.pipe(distinctUntilChanged())
// output: 1,2,3
.subscribe(console.log);Example 2: distinctUntilChanged with objects
( StackBlitz )
Example 3: Using custom comparer function
( StackBlitz )
Related Recipes
Additional Resources
distinctUntilChanged ๐ฐ - Official docs
distinctUntilChanged - In Depth Dev Reference
Filtering operator: distinct and distinctUntilChanged ๐ฅ ๐ต - Andrรฉ Staltz
๐ Source Code: https://github.com/ReactiveX/rxjs/blob/master/src/internal/operators/distinctUntilChanged.ts
Last updated