...

/

Filtering Operators - Part 1

Filtering Operators - Part 1

Deep dive into filtering operators that will allow you to build powerful reactive streams.

From now on, we will examine the data pipeline by applying different operators that will filter, transform, and combine the flowing data. In this section, we will analyze the most important filtering operators.

The data pipeline in RxJS must be defined inside the pipe() function, as shown below:

Observable
.pipe(
 operator1,
 operator2,
 operator3,
 ...
)



filter

As the name suggests, the filter operator expects a predicate function, that applies to the value passing through the stream. Only the values for which the function returns true are allowed to flow through.

The following example shows an application of the filter operator that only allows even numbers to reach the Observer.

Press + to interact
demo
main.js
const { from } = require('rxjs');
const { filter } = require('rxjs/operators');
const observable$ = from([1, 2, 3, 4, 5, 6])
observable$.pipe(
filter(val => val % 2 == 0)
).subscribe(console.log)



...