Learn RxJS
Search…
distinctUntilKeyChanged

signature: distinctUntilKeyChanged(key, compare: fn): Observable

Only emit when the specified key value has changed

Examples

Example 1: Compare based on key
1
// RxJS v6+
2
import { from } from 'rxjs';
3
import { distinctUntilKeyChanged } from 'rxjs/operators';
4
5
// only output distinct values, based on the last emitted value
6
const source$ = from([
7
{ name: 'Brian' },
8
{ name: 'Joe' },
9
{ name: 'Joe' },
10
{ name: 'Sue' }
11
]);
12
13
source$
14
// custom compare based on name property
15
.pipe(distinctUntilKeyChanged('name'))
16
// output: { name: 'Brian }, { name: 'Joe' }, { name: 'Sue' }
17
.subscribe(console.log);
Copied!
Example 2: Keyboard events
1
// RxJS v6+
2
import { fromEvent } from 'rxjs';
3
import { distinctUntilKeyChanged, pluck } from 'rxjs/operators';
4
5
const keys$ = fromEvent(document, 'keyup').pipe(
6
distinctUntilKeyChanged < KeyboardEvent > 'code',
7
pluck('key')
8
);
9
10
keys$.subscribe(console.log);
Copied!

Additional Resources

Last modified 1yr ago