Skip to content
beginner Phase 10 · RxJS Operators for Angular

map, filter, and reduce Operators

Reshape and narrow emissions as they flow through a pipe.

40m
0 problems
Topic Progress 0%

Transformation Operators

Operators transform, filter, and combine Observable streams. They are the core of reactive programming in Angular.

map — Transform Each Value

import { map } from 'rxjs/operators';

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

numbers$.pipe(
  map(n => n * 2)
).subscribe(result => console.log(result));
// Output: 2, 4, 6, 8, 10

filter — Drop Values Failing a Predicate

import { filter } from 'rxjs/operators';

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

reduce — Accumulate to a Single Value

import { reduce } from 'rxjs/operators';

numbers$.pipe(
  reduce((acc, n) => acc + n, 0)
).subscribe(result => console.log(result));
// Output: 15

pipe — Chaining Operators

import { of } from 'rxjs';
import { map, filter, reduce } from 'rxjs/operators';

of(1, 2, 3, 4, 5).pipe(
  filter(n => n % 2 === 0),  // Keep even numbers: 2, 4
  map(n => n * 10),            // Multiply by 10: 20, 40
  reduce((acc, n) => acc + n, 0) // Sum: 60
).subscribe(result => console.log(result));
// Output: 60

Practical Angular Example

this.http.get<User[]>('/api/users').pipe(
  map(users => users.filter(u => u.isActive)),
  map(users => users.map(u => u.name))
).subscribe(names => {
  this.activeUserNames = names;
});

More Transformation Operators

Beyond map, filter, and reduce, RxJS provides many operators for common transformations.

tap — Side Effects

import { tap } from 'rxjs/operators';

this.data$.pipe(
  tap(data => console.log('Received:', data)),
  map(data => data.items)
).subscribe();

take — Limit Emissions

import { take } from 'rxjs/operators';

// Take only the first 3 values
this.data$.pipe(
  take(3)
).subscribe();

skip — Skip Emissions

import { skip } from 'rxjs/operators';

// Skip the first 2 values
this.data$.pipe(
  skip(2)
).subscribe();

distinctUntilChanged — Deduplicate

import { distinctUntilChanged } from 'rxjs/operators';

this.search$.pipe(
  debounceTime(300),
  distinctUntilChanged()
).subscribe(query => {
  this.search(query);
});

scan — Running Accumulation

import { scan } from 'rxjs/operators';

// Like reduce, but emits intermediate values
numbers$.pipe(
  scan((acc, n) => acc + n, 0)
).subscribe(runningTotal => console.log(runningTotal));
// Output: 1, 3, 6, 10, 15

Common Operator Categories

Category Operators
Transformation map, scan, bufferTime
Filtering filter, take, skip, distinctUntilChanged
Combination mergeMap, switchMap, concatMap, combineLatest
Utility tap, delay, timeout
Error handling catchError, retry, retryWhen
Multicasting share, shareReplay

Quiz

1. What does the map operator do?

Question 1 options

2. What does the filter operator do?

Question 2 options

3. What is the difference between reduce and scan?

Question 3 options

Flashcards

Question

What does map do?

Answer

Transforms each emitted value through a function.

Question

What does filter do?

Answer

Drops values that fail a predicate.

Question

reduce vs scan?

Answer

reduce emits final value only. scan emits intermediate accumulations.

Revision Notes

Key Takeaways

  • 1. map transforms each value
  • 2. filter drops values failing a predicate
  • 3. reduce accumulates to one final value
  • 4. pipe chains operators left to right

Interview Tips

  • • Explain the difference between reduce and scan
  • • Be ready to describe common operator categories
  • • Mention practical Angular use cases

Cheat Sheet

Transform Operators Cheat Sheet

map:

map(n => n * 2)

filter:

filter(n => n % 2 === 0)

reduce:

reduce((acc, n) => acc + n, 0)

Chaining:

source$.pipe(
  filter(...),
  map(...),
  reduce(...)
)

Continue Learning