RxJS Transformation & Filtering
Learn how RxJS operators transform Observable values and filter unwanted data before it reaches the subscriber.
What are Transformation and Filtering?
Transformation means changing each emitted value into another value or format.
Filtering means allowing only those values that satisfy a specific condition to continue through the stream.
RxJS provides operators such as map() for transformation and filter() for filtering.
Transformation and Filtering Flow
What is Transformation?
Transformation changes the data emitted by the source Observable into another form.
For example, numbers can be multiplied, strings can be formatted, or objects can be converted into another structure.
map() Operator
The map() operator applies a transformation function to every value emitted by the source Observable.
The transformed value is emitted by the resulting Observable.
const numbers$ = of(
10,
20,
30
);
const result$ = numbers$.pipe(
map(value => value * 2)
);
result$.subscribe({
next: value => {
console.log(value);
}
});
Output
20
40
60
map() Transformation Flow
String Transformation
map() can also transform strings into another format.
const courses$ = of(
'Java',
'.NET',
'Python'
);
const fullStackCourses$ =
courses$.pipe(
map(course =>
course + ' Full Stack'
)
);
fullStackCourses$.subscribe({
next: course => {
console.log(course);
}
});
Output
Java Full Stack
.NET Full Stack
Python Full Stack
Object Transformation
map() can also extract or create a different value from an object emitted by an Observable.
This is useful when the application needs only a specific property from a larger object.
const courses$ = of(
{
name: 'Java Full Stack',
duration: '6 Months'
},
{
name: '.NET Full Stack',
duration: '6 Months'
}
);
const names$ = courses$.pipe(
map(course => course.name)
);
names$.subscribe({
next: name => {
console.log(name);
}
});
What is Filtering?
Filtering means selecting only the values that match a specified condition.
RxJS provides the filter() operator for this purpose.
filter() Operator
The filter() operator checks every emitted value against a condition.
Only values for which the condition is true continue through 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
Filtering Strings
const courses$ = of(
'Java',
'.NET',
'Python',
'Angular'
);
const angularCourses$ =
courses$.pipe(
filter(course =>
course === 'Angular'
)
);
angularCourses$.subscribe({
next: course => {
console.log(course);
}
});
Output
Angular
Filtering Objects
Objects can be filtered using one or more of their properties.
This is useful when the application wants to display only matching records.
const courses$ = of(
{
name: 'Java Full Stack',
active: true
},
{
name: '.NET Full Stack',
active: false
},
{
name: 'Python Full Stack',
active: true
}
);
const activeCourses$ =
courses$.pipe(
filter(course =>
course.active
)
);
Combining map() and filter()
Transformation and filtering can be combined in the same RxJS pipeline.
The order of operators matters because each stage processes the output from the previous stage.
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);
}
});
Output
30
40
50
Operator Order Matters
Filtering Before Transformation
const numbers$ = of(
10,
20,
30,
40,
50
);
const result$ = numbers$.pipe(
filter(value =>
value >= 30
),
map(value =>
value / 10
)
);
result$.subscribe({
next: value => {
console.log(value);
}
});
Output
3
4
5
map() vs filter()
| Feature | map() | filter() |
|---|---|---|
| Purpose | Transform values | Select values |
| Output | Transformed value | Original matching value |
| Condition | Not required | Usually required |
| Example | value * 2 | value > 10 |
| Main Use | Change data | Keep required data |
CIIT Training Institute Example
Suppose CIIT Training Institute has a stream of course information containing course name and fees.
We can first select courses above a particular fee and then transform the matching objects into display-friendly text.
const courses$ = of(
{
name: 'Java Full Stack',
fees: 25000
},
{
name: '.NET Full Stack',
fees: 35000
},
{
name: 'Python Full Stack',
fees: 40000
}
);
const premiumCourses$ =
courses$.pipe(
filter(course =>
course.fees >= 35000
),
map(course =>
course.name +
' - ₹' +
course.fees
)
);
premiumCourses$.subscribe({
next: course => {
console.log(course);
}
});
CIIT Training Institute
.NET Full Stack - ₹35000
Python Full Stack - ₹40000
Practical Search Example
A common use case is filtering search results and then transforming the selected data for display.
For example, a course search can keep only matching courses and then return their names.
const courses$ = of(
'Java Full Stack',
'.NET Full Stack',
'Python Full Stack',
'Data Analytics'
);
const result$ = courses$.pipe(
filter(course =>
course.includes('Full Stack')
),
map(course =>
course.toUpperCase()
)
);
result$.subscribe({
next: course => {
console.log(course);
}
});
Search Result
JAVA FULL STACK
.NET FULL STACK
PYTHON FULL STACK
Transformation & Filtering Best Practices
-
Use
map()when the emitted value needs to be transformed. -
Use
filter()when only values matching a condition should continue. - Keep transformation functions simple and readable.
- Consider the order of operators when multiple operators are chained.
- Avoid unnecessary transformations when the source data is already in the required format.
- Keep complex business rules outside the presentation layer where practical.
Important Points
map() transforms each emitted value into another value.
filter() allows only values that satisfy its condition.
map() and filter() can be combined in a single pipe() pipeline.
Each operator receives the output produced by the previous stage in the pipeline.
Summary
RxJS transformation changes emitted values into another form using operators such as map().
Filtering selects only the values that satisfy a condition using filter().
These operators can be combined inside pipe() to create a useful data-processing pipeline. In the next topic, we will learn about Combining Observables.