skip to content

map

 

map turns each emitted value into another value, using mapping function

Also check out pluck operator and try comparing map to pluck

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
const { rxObserver } = require('api/v0.3');
const { timer } = require('rxjs');
const { map, take } = require('rxjs/operators');

// 5 values from a timer
timer(0, 10)
  .pipe(
    take(5)
  )
  .subscribe(rxObserver('timer(0, 5)'));

// 5 mapped values from a timer
timer(0, 10)
  .pipe(
    map(i => i + ' 🦆'),
    take(5)
  )
  .subscribe(rxObserver('map(i => i + 🦆)'));

0mstimer(0, 5)startcomplete00 11 22 33 44 map(i => i + 🦆)startcomplete0 🦆0 🦆 1 🦆1 🦆 2 🦆2 🦆 3 🦆3 🦆 4 🦆4 🦆

NOTE: if your mapping function returns an Observable or a Promise — you'll need one of *Map operators:
mergeMap, exhaustMap, switchMap, concatMap

NOTE 2: I've created a package rxjs/proxy {👓} to simplify subproperty access:
It turns Observables of Objects into Objects of Observables
And rxjs/autorun package will let you easily map a single stream or combine multiple.