Member-only story
More Rxjs Transformation Operators — Scan and Window
Rxjs is a library for doing reactive programming. Creation operators are useful for generating data from various data sources to be subscribed to by Observers.
In this article, we’ll look at how to use transformation operators scan
, switchMap
, switchMapTo
and window
.
scan
The scan
operator applies an accumulator function over the source Observation by combining the emitted values. Then it returns each intermediate result, with an optional seed value.
It takes up to 2 arguments. The first is the accumulator
function, which is the function that combines the value that was accumulated so far with the new emitted value.
The second is an optional argument, which is the seed
, or the initial accumulation value.
For example, we can use it as follows:
import { of } from "rxjs";
import { scan } from "rxjs/operators";const of$ = of(1, 2, 3);
const seed = 0;
const count = of$.pipe(scan((acc, val) => acc + val, seed));
count.subscribe(x => console.log(x));
The code above starts with an of
Observable and a seed
initial value of 0. Then we pipe
the values from the of$
Observable to the scan
operator, which has the accumulation function, which adds the…