AI Hints
Home›Angular Tutorial›RxJS Basics, map and filter and Search with debounceTime
Angular · Lesson 101 of 120

map and filter

Operators.

⏱ 2 min read▶ Video at 2:02:12🎓 Free
Watch this lesson in the Angular courseWatch on YouTube ↗Full Angular course

Operators change a stream of values, step by step. They go inside pipe, and run in order.

  • Change a stream
  • Step by step
  • pipe()

Common operators

Map changes each value. Filter keeps only some values. Tap looks at values, without changing them. Take stops after a number of values. And switch map swaps each value for a new observable, like an HTTP request.

OperatorWhat it does
map(fn)change each value
filter(fn)keep some values
tap(fn)look, don't change
take(n)stop after n
switchMap(fn)switch to a new observable

filter, then map

From sends three books. Filter keeps the books under ten pounds. Then map turns each one into a line of text. Run it. Circe was filtered out. And the other two became text.

main.tstypescript
import { from, map, filter } from 'rxjs';

const books = [
  { title: 'Dune', price: 9.99 },
  { title: 'Circe', price: 10.99 },
  { title: 'Emma', price: 6.99 },
];

from(books).pipe(
  filter(book => book.price < 10),
  map(book => `${book.title}: £${book.price}`),
).subscribe(line => console.log(line));
Output
Dune: £9.99
Emma: £6.99
Also uses _mode
ts

Summary

Pipe runs operators in order. Filter keeps some values, and map changes them. Next, a search box, with debounce time.

Key takeaways
  • pipe()
  • filter()
  • map()
Next up: Search with debounceTime — wait for the user