Home » RxJS distinct() Filtering Operator

RxJS distinct() Filtering Operator

by Online Tutorials Library

RxJS distinct() Filtering Operator

RxJS distinct() operator is a filtering operator that returns all the values from the source Observable that are distinct when compared with the previous values.

In the RxJS distinct() operator, if a keySelector function is given, it will project each value from the source observable into a new value that will check for equality with previously projected values. If a keySelector function is not given, it will use each value from the source observable directly with an equality check against previous values.

Syntax:

Following is the syntax of the distinct() operator:

Or

Parameter Explanation

  • keySelector: It specifies an optional function to select which value you want to check as distinct. It is an optional argument. Its default value is undefined.
  • flushes: It specifies an optional observable for flushing the internal HashSet of the operator. It is also an optional argument. Its default value is undefined.

Return value

The RxJS distinct() operator’s return value is observable that emits items from the source observable with distinct values.

Let us see some examples of distinct() operator to understand it clearly.

Example 1 (Distinct Values without Selector)

Output:

After executing the above example, you will see the following result as array:

RxJS distinct() Filtering Operator

In the above example, you can see that we have used the repeated values as input but this operator has emitted only the distinct values as output.

Example 2 (Distinct values with key selector)

Output:

After executing the above example, you will see the following result as array:

RxJS distinct() Filtering Operator


Next TopicRxJS Operators

You may also like