Skip to content
the poor coder

rxjs map

Poor Man
Published
Reading time
2 min read
On this page

RxJS Map Function#

RxJS is a library for reactive programming using Observables. The map operator is one of the most commonly used operators in RxJS. It is used to transform the data emitted by an Observable. The map operator applies a function to each value emitted by the Observable and returns a new Observable that emits the transformed values.

Syntax#

JavaScript
// Using pipeable operator syntax
observable$.pipe(
  map(value => transformFunction(value))
);

// Using the method chain syntax
observable$.map(value => transformFunction(value));

The map function takes a function as an argument which will be applied to each value emitted by the Observable. The function should return the transformed value to be emitted by the new Observable.

Example#

Let's say we have an Observable that emits a stream of numbers:

JavaScript
// Create an Observable that emits numbers
const numbers$ = of(1, 2, 3, 4, 5);

// Print out the original numbers
numbers$.subscribe(value => console.log(value));

The output of the above code will be:

JavaScript
1
2
3
4
5

We can use the map operator to transform the numbers into their square:

JavaScript
// Transform the numbers using the map operator
const squaredNumbers$ = numbers$.pipe(
  map(value => value * value)
);

// Print out the squared numbers
squaredNumbers$.subscribe(value => console.log(value));

The output of the above code will be:

JavaScript
1
4
9
16
25

As you can see, the map operator transformed each number into its square and the new Observable emitted the transformed values.

Multiple Ways to Use Map Operator#

There are multiple ways to use the map operator in RxJS. You can use it with the pipeable operator syntax or with the method chain syntax. Here's an example of using map with the method chain syntax:

JavaScript
// Using map with the method chain syntax
const doubledNumbers$ = numbers$.map(value => value * 2);

// Print out the doubled numbers
doubledNumbers$.subscribe(value => console.log(value));

The output of the above code will be:

JavaScript
2
4
6
8
10

As you can see, we used the map operator with the method chain syntax to double each number emitted by the Observable.

Conclusion#

The RxJS map operator is a powerful tool for transforming data emitted by Observables. It can be used in a variety of ways to transform data into any format you need. With its easy-to-use syntax and flexibility, it's no wonder why the map operator is one of the most commonly used operators in RxJS.

Keep going

2023

how to sort an array

How to Sort an Array If you have an array and you want to order its elements in a specific way, you need to use a sorting algorithm. There are several sorting algorithms available, but two of the most commonly used are…

javascript2 min read
2023

jquery mobile or desktop

jQuery Mobile or Desktop? As a web developer, I have had some experience with both jQuery Mobile and jQuery Desktop. Both frameworks have their pros and cons, and which one to use really depends on the specific project…

javascript1 min read
2023

node.js folder structure

Node.js Folder Structure If you are new to Node.js, it can be daunting to create a folder structure that will work for your project. The structure you choose will depend on the size and complexity of your project. Here…

javascript1 min read
ESC