Contents

map operator in RxJs

Updated on 2021-02-09

Map Operator

Can be imported from here:

1
2
3
import { map } from 'rxjs/operators';
// or
const { map } = require('rxjs/operators');

It functions in a similar fashion to Array.prototype.map(). It receives values from passing observable and modifies it as intended in the mapping function.

To understand interval operator, visit interval operator

1
2
3
4
5
6
7
8
const { interval } = require('rxjs');
const { map } = require('rxjs/operators');

const interval$ = interval(1000);

interval$.pipe(map((val) => val * 1000)).subscribe((value) => {
    console.log(value);
})

Output:

1
2
3
4
5
6
7
8
0
1000
2000
3000
4000
5000
6000
// ... and so on
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
const { interval } = require('rxjs');
const { map } = require('rxjs/operators');

const interval$ = interval(1000);

interval$.pipe(
    map((val) => {
        return {
            value: val * 1000
        }
    }
)).subscribe((value) => {
    console.log(value);
})

Output:

1
2
3
4
5
6
7
8
9
{value: 0}
{value: 1000}
{value: 2000}
{value: 3000}
{value: 4000}
{value: 5000}
{value: 6000}
{value: 7000}
// ... and so on

Комментарии