Efficient-angular
GitHubToggle Dark/Light/Auto modeToggle Dark/Light/Auto modeToggle Dark/Light/Auto modeBack to homepage

Distinct

Returns an Observable that emits all items emitted by the source Observable that are distinct by comparison from previous items.

Syntax

distinct(keySelector?, flushes?): Observable
ParametersDescription
keySelector OptionalDefault is undefined. Function to select which value you want to check as distinct.
flushes OptionalDefault is undefined. Observable for flushing the internal HashSet of the operator.

Returns

MonoTypeOperatorFunction: A function that returns an Observable that emits items from the source Observable with distinct values.

Example

import { of } from 'rxjs';
import { distinct } from 'rxjs/operators';
 
interface Person {
   age: number,
   name: string
}
 
of(
    { age: 4, name: 'Foo'},
    { age: 7, name: 'Bar'},
    { age: 5, name: 'Foo'}
  ).pipe(
    distinct((p: Person) => p.name)
  )
  .subscribe(x => console.log(x));
 
// Outputs
// { age: 4, name: 'Foo' }
// { age: 7, name: 'Bar' }