All topics
RxJSbeginner

The map and filter Operators

Explain how map and filter transform and select emitted values without mutating the source Observable.

map and filter are usually the first two RxJS operators anyone learns, precisely because they mirror the exact same array methods with the exact same names and semantics — map transforms each emitted value into something else, and filter selectively lets some values through based on a predicate, discarding the rest. Interviewers ask about these first because they establish the foundational operator mental model (pure, composable transformations piped together) before moving into the more complex flattening operators.

map is like a translator converting every sentence spoken in one room to another language before it reaches your ears; filter is like a bouncer at a door only letting through guests who match a specific criterion, turning everyone else away before they even reach the room.

Key Concepts

1
Both operators are "pipeable" — used inside an Observable's .pipe() method, which is the standard way RxJS composes multiple operators together into a single processing pipeline that a source Observable's values flow through, in order, before reaching the final subscriber. Crucially, neither operator mutates the source Observable or its emitted values; each returns a brand-new Observable representing the transformed/filtered stream, leaving the original untouched — the same immutability principle behind array methods like .map()/.filter() on a plain array.
.pipe().map().filter()
2
map receives each emitted value (and, like the array version, an optional index) and returns a new value to emit in its place — a direct one-to-one transformation, unlike the flattening operators (switchMap, mergeMap, and so on) which return an entirely new Observable per input value rather than a plain transformed value.
mapswitchMapmergeMap
3
A good interview answer notes that because map and filter are trivially composable and easy to test in isolation, they're often the first operators reached for when refactoring a messy nested .subscribe() callback into a clean, declarative pipeline — a broader RxJS best practice worth mentioning: prefer composing operators in .pipe() over doing transformation/filtering logic inside the subscriber callback itself.
mapfilter.subscribe().pipe()