Course contentsShow
← JavaScript
Lesson 2132 of 2,72344. Reactive PatternsFree lesson

Operators: Map and Filter

Transforming and filtering values in a stream using the map and filter operators.

Operators: Map and Filter

You'll learn how to transform and selectively process values flowing through RxJS observables using the map and filter operators.

The Core Idea

In RxJS, operators are functions that take an observable and return a new observable with modified behavior. The map operator transforms each emitted value, while filter lets only certain values pass through — just like their array counterparts, but working over time with asynchronous streams.

Think of a conveyor belt carrying packages. The map operator is like a station that opens each box, transforms its contents, and repackages it. The filter operator is like a quality checkpoint that only lets certain packages continue down the line.

Using Map

The map operator applies a transformation function to every value:

import { of } from 'rxjs';
import { map } from 'rxjs/operators';

const numbers$ = of(1, 2, 3, 4, 5);

numbers$.pipe(
  map(x => x * 10)
).subscribe(value => console.log(value));
// Output: 10, 20, 30, 40, 50

Using Filter

The filter operator only emits values that satisfy a condition:

import { of } from 'rxjs';
import { filter } from 'rxjs/operators';

const numbers$ = of(1, 2, 3, 4, 5);

numbers$.pipe(
  filter(x => x % 2 === 0)
).subscribe(value => console.log(value));
// Output: 2, 4

Chaining Together

You can combine operators for powerful stream transformations:

numbers$.pipe(
  filter(x => x > 2),
  map(x => x * x)
).subscribe(value => console.log(value));
// Output: 9, 16, 25

Key Takeaway: The map and filter operators transform observable streams just like array methods, but they work on values emitted over time, creating new observables without mutating the original stream.