π‘ distinctUntilChanged uses ===
comparison by default, object references must match!
π‘ If you want to compare based on an object property, you can use distinctUntilKeyChanged
instead!
ββββ
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 valueconst source$ = from([1, 1, 2, 2, 3, 3]);βsource$.pipe(distinctUntilChanged())// output: 1,2,3.subscribe(console.log);
Example 2: distinctUntilChanged with objects
( StackBlitz )
// RxJS v6+import { from } from 'rxjs';import { distinctUntilChanged } from 'rxjs/operators';βconst sampleObject = { name: 'Test' };β//Objects must be same referenceconst source$ = from([sampleObject, sampleObject, sampleObject]);β// only emit distinct objects, based on last emitted valuesource$.pipe(distinctUntilChanged())// output: {name: 'Test'}.subscribe(console.log);
Example 3: Using custom comparer function
( StackBlitz )
// RxJS v6+import { from } from 'rxjs';import { distinctUntilChanged } from 'rxjs/operators';β// only output distinct values, based on the last emitted valueconst source$ = from([{ name: 'Brian' },{ name: 'Joe' },{ name: 'Joe' },{ name: 'Sue' }]);βsource$// custom compare for name.pipe(distinctUntilChanged((prev, curr) => prev.name === curr.name))// output: { name: 'Brian }, { name: 'Joe' }, { name: 'Sue' }.subscribe(console.log);
βLockscreenβ
βSave Indicatorβ
βType Aheadβ
βdistinctUntilChangedβ
π° - Official docs
βFiltering operator: distinct and distinctUntilChangedβ
π₯ π΅ - AndrΓ© Staltz
π Source Code: https://github.com/ReactiveX/rxjs/blob/master/src/internal/operators/distinctUntilChanged.tsβ