Learn RxJS
Search…
mapTo

signature: mapTo(value: any): Observable

Map emissions to constant value.

New to transformation operators? Check out the article Get started transforming streams with map, pluck, and mapTo!

Examples

Example 1: Map every emission to string
1
// RxJS v6+
2
import { interval } from 'rxjs';
3
import { mapTo } from 'rxjs/operators';
4
5
//emit value every two seconds
6
const source = interval(2000);
7
//map all emissions to one value
8
const example = source.pipe(mapTo('HELLO WORLD!'));
9
//output: 'HELLO WORLD!'...'HELLO WORLD!'...'HELLO WORLD!'...
10
const subscribe = example.subscribe(val => console.log(val));
Copied!
Example 2: Mapping clicks to string
1
// RxJS v6+
2
import { fromEvent } from 'rxjs';
3
import { mapTo } from 'rxjs/operators';
4
5
//emit every click on document
6
const source = fromEvent(document, 'click');
7
//map all emissions to one value
8
const example = source.pipe(mapTo('GOODBYE WORLD!'));
9
//output: (click)'GOODBYE WORLD!'...
10
const subscribe = example.subscribe(val => console.log(val));
Copied!

Additional Resources

Last modified 1yr ago