Demo: map
With the map operator we can apply a one-on-one transformation logic to the incoming values and emit the resulting values.
map(v => functionToTransform);
let input = timer(500,1500).pipe(take(4));
//Transform the value and emit into the resulting stream
let process=map(v => v+100);
let output=input.pipe(process).subscribe(v=>console.log(v))
More on :
map
Using map operator, we can apply any kind of one-on-one mapping to our incoming values.
After mapping the operator adds the mapped value to the output stream.
1. Mapping to Observables
Incase we are mapping to observables, it will emit those observables into the output stream.
But, most often we do not want those mapped Observables but, want their outcomes in certain ways. To make it easy, RxJs provides methods like - concatMap , mergeMap, switchMap etc.
These methods allow us to map to our Observables and, then, subscribe to them in our desired ways. For instance,
operators | map | + | Specific way to subscribe | |
---|---|---|---|---|
map | = | map | + | emit the Observables into the output stream |
concatMap | = | map | + | concat the output of the Observables, by running them in sequence. |
mergeMap | = | map | + | merge the output of the Observables, by running them in parallel |
switchMap | = | map | + | switch to the latest Observable |
exhaustMap | = | map | + | exhaust (or discard) the concurrent Observables |