Learn RxJS
Search…
distinct

signature: distinct(keySelector?, flushes?): Observable

Emits items emitted that are distinct based on any previously emitted item.

Examples

Example 1: distinct without selector
1
// RxJS v6+
2
import { of } from 'rxjs';
3
import { distinct } from 'rxjs/operators';
4
5
of(1, 2, 3, 4, 5, 1, 2, 3, 4, 5)
6
.pipe(distinct())
7
// OUTPUT: 1,2,3,4,5
8
.subscribe(console.log);
Copied!
Example 2: distinct with key selector
1
// RxJS v6+
2
import { from } from 'rxjs';
3
import { distinct } from 'rxjs/operators';
4
5
const obj1 = { id: 3, name: 'name 1' };
6
const obj2 = { id: 4, name: 'name 2' };
7
const obj3 = { id: 3, name: 'name 3' };
8
const vals = [obj1, obj2, obj3];
9
10
from(vals)
11
.pipe(distinct(e => e.id))
12
.subscribe(console.log);
13
14
/*
15
OUTPUT:
16
{id: 3, name: "name 1"}
17
{id: 4, name: "name 2"}
18
*/
Copied!

Additional Resources