( StackBlitz )
// RxJS v6+
import { from } from 'rxjs';
import { distinctUntilKeyChanged } from 'rxjs/operators';
// only output distinct values, based on the last emitted value
const source$ = from([
{ name: 'Brian' },
{ name: 'Joe' },
{ name: 'Joe' },
{ name: 'Sue' }
]);
source$
// custom compare based on name property
.pipe(distinctUntilKeyChanged('name'))
// output: { name: 'Brian }, { name: 'Joe' }, { name: 'Sue' }
.subscribe(console.log);
( StackBlitz )
// RxJS v6+
import { fromEvent } from 'rxjs';
import { distinctUntilKeyChanged, pluck } from 'rxjs/operators';
const keys$ = fromEvent(document, 'keyup').pipe(
distinctUntilKeyChanged < KeyboardEvent > 'code',
pluck('key')
);
keys$.subscribe(console.log);
- distinctUntilKeyChanged 📰 - Official docs
📁 Source Code: https://github.com/ReactiveX/rxjs/blob/master/src/internal/operators/distinctUntilKeyChanged.ts