Angular Observable Operators
Learn how RxJS operators can transform, filter, inspect and control values flowing through an Observable stream.
What are Observable Operators?
Operators are functions that allow us to work with Observable streams without changing the original source directly.
Operators can transform values, filter unwanted values, perform side effects, limit emissions and combine different operations into a data-processing pipeline.
RxJS provides many operators that can be used with pipe().
Observable Operator Flow
Using pipe()
The pipe() method is used to connect one or more RxJS operators together.
The output of one operator becomes the input for the next operator in the pipeline.
const result$ = source$.pipe(
operator1(),
operator2()
);
Common RxJS Operators
map()
Transforms every emitted value into another value.
filter()
Allows only values that satisfy a condition.
tap()
Performs a side effect without changing the value.
take()
Takes a specified number of emitted values.
1. map() Operator
The map() operator transforms every value emitted by the source Observable.
It is useful when the application needs to convert the incoming data into another format.
const numbers$ = of(10, 20, 30);
const doubled$ = numbers$.pipe(
map(value => value * 2)
);
doubled$.subscribe({
next: value => {
console.log(value);
}
});
Output
20
40
60
map() Transformation Flow
2. filter() Operator
The filter() operator allows only those values that satisfy a specified condition.
Values that fail the condition are not emitted by the resulting Observable.
const numbers$ = of(10, 15, 20, 25, 30);
const evenNumbers$ = numbers$.pipe(
filter(value => value % 2 === 0)
);
evenNumbers$.subscribe({
next: value => {
console.log(value);
}
});
Filtered Output
10
20
30
3. tap() Operator
The tap() operator is useful for performing side effects such as logging or debugging.
It does not transform the value being emitted.
const courses$ = of(
'Java',
'.NET',
'Python'
);
const result$ = courses$.pipe(
tap(course => {
console.log(
'Received:',
course
);
})
);
result$.subscribe({
next: course => {
console.log(
'Display:',
course
);
}
});
4. take() Operator
The take() operator allows only a specified number of values to pass through.
After the requested number of values is emitted, the resulting Observable completes.
const numbers$ = of(
1,
2,
3,
4,
5
);
const firstThree$ = numbers$.pipe(
take(3)
);
firstThree$.subscribe({
next: value => {
console.log(value);
}
});
Output
1
2
3
Using Multiple Operators
Multiple operators can be combined inside a single pipe().
This allows an Observable to pass through several processing steps before the subscriber receives the final values.
const numbers$ = of(
1,
2,
3,
4,
5
);
const result$ = numbers$.pipe(
map(value => value * 10),
filter(value => value >= 30)
);
result$.subscribe({
next: value => {
console.log(value);
}
});
Processing Result
30
40
50
Operator Pipeline
CIIT Training Institute Example
Suppose the CIIT Training Institute dashboard receives a list of course enquiry priorities.
We can use operators to transform the enquiry data and keep only the records needed by the dashboard.
const fees$ = of(
15000,
25000,
35000,
45000
);
const premiumCourses$ = fees$.pipe(
filter(fee => fee >= 30000),
map(fee => 'Course Fee: ₹' + fee)
);
premiumCourses$.subscribe({
next: fee => {
console.log(fee);
}
});
CIIT Training Institute
Course Fee: ₹35000
Course Fee: ₹45000
Common Operators Overview
| Operator | Purpose | Example |
|---|---|---|
map()
|
Transforms every value | value * 2 |
filter()
|
Filters values based on a condition | value > 10 |
tap()
|
Performs a side effect | Logging |
take()
|
Takes a fixed number of values | take(3) |
pipe()
|
Combines operators | pipe(map(), filter()) |
Other Useful Operators
distinctUntilChanged()
Suppresses consecutive duplicate values.
skip()
Skips a specified number of initial emissions.
debounceTime()
Delays emissions until the source remains quiet for a period.
catchError()
Provides a way to handle errors in an Observable pipeline.
Operator Categories
Transformation
Changes emitted values, such as map().
Filtering
Controls which values continue through the stream.
Combination
Works with multiple Observable streams.
Error Handling
Handles failures in Observable processing.
Important Points
Operators return another Observable representing the processed stream.
Operators are commonly connected using the pipe() method.
Multiple operators can be used in sequence to build a processing pipeline.
Use transformation, filtering, timing, combination and error-handling operators according to the stream logic.
Operator Best Practices
- Keep operator pipelines easy to read and understand.
- Use map() when data needs transformation.
- Use filter() when only matching values should continue.
- Use tap() mainly for side effects such as logging or debugging.
- Use suitable operators instead of manually handling every stream value.
- Keep complex stream logic separated from presentation logic where practical.
Summary
RxJS operators allow Angular applications to transform, filter and control values flowing through Observable streams.
Operators are commonly combined using pipe().
Important operators include map(), filter(), tap() and take(). In the next topic, we will learn about Unsubscribe.