Home » RxJS mapTo() Transformation Operator

RxJS mapTo() Transformation Operator

by Online Tutorials Library

RxJS mapTo() Transformation Operator

RxJS mapTo() operator is a transformation operator that emits a constant value as an output along with the Observable whenever the source Observable emits a value.

Like a map() operator, it maps every source value to the same output value every time.

Syntax:

Following is the syntax of the mapTo() operator:

Parameter Explanation

value: It specifies a value as an argument which is mapped to the source value given.

Return value

The mapTo() operator’s return value is Observable that emits the given value every time the source Observable emits something.

Let us see some examples of mapTo() operator to understand it clearly.

Example 1 (Map every emission to String)

Output:

After executing the above example, you will see the following result as array:

RxJS mapTo() Transformation Operator

In the above example, you can see that every value is mapped to string which is emitted after three seconds.

Example 2 (Mapping clicks to string)

Output:

After executing the above example, you will see the following result as array:

RxJS mapTo() Transformation Operator

In the above example, you can see that it has mapped every click to the string ‘Hello World….Welcome to TutorAspire!’ which is displayed on the console as output.


Next TopicRxJS Operators

You may also like