Javatpoint Logo
Javatpoint Logo

RxJS filter() Filtering Operator

RxJS filter() operator is a filtering operator used to filter items emitted by the source observable according to the predicate function. It only emits those values that satisfy a specified predicate.

The RxJS filter() operator is like the well-known Array Array .prototype.filter() method. This operator takes values from the source Observable, passes them through a predicate function and only emits those values that get TRUE.

Syntax:

Following is the syntax of the RxJS filter() operator:

Or

Parameter Explanation

  • predicate_func: The predicate_func returns a boolean value, and the output will get filtered if the function returns a truthy value.
  • predicate: It is used to specify a function that evaluates each value emitted by the source observable. If it returns true, the value is emitted, if false, the value is not passed to the output observable. The index parameter is the number i for the i-th source emission that has happened since the subscription, starting from the number 0.
  • thisArg: It is an optional argument used to determine the value of this in the predicate function. Its default value is undefined.

Return value

The RxJS filter() operator's return value is observable of values from the source observable satisfied by the predicate function.

Let us see some examples of the RxJS filter() operator to understand it clearly.

Example 1 (Filter for even numbers)

Output:

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

RxJS filter() Filtering Operator

In the above example, you can see that we have filtered the even numbers using the filter() operator.

Example 2 (Filter objects according to their property)

Output:

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

RxJS filter() Filtering Operator

Here, you can see that the RxJS filter() operator has emitted peoples only under the age of 25.

Example 3 (Filter numbers greater than a specified value after an interval)

Output:

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

RxJS filter() Filtering Operator

In the above example, you can see that the RxJS filter() operator has filtered out all values until the interval is greater than 3.


Next TopicRxJS Operators




Youtube For Videos Join Our Youtube Channel: Join Now

Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA