scan
signature: scan(accumulator: function, seed: any): Observable
scan(accumulator: function, seed: any): ObservableReduce over time.
๐ก You can create Redux-like state management with scan!
Examples
Example 1: Sum over time
( StackBlitz )
// RxJS v6+
import { of } from 'rxjs';
import { scan } from 'rxjs/operators';
const source = of(1, 2, 3);
// basic scan example, sum over time starting with zero
const example = source.pipe(scan((acc, curr) => acc + curr, 0));
// log accumulated values
// output: 1,3,6
const subscribe = example.subscribe(val => console.log(val));Example 2: Accumulating an object
( StackBlitz | jsBin | jsFiddle )
Example 3: Emitting random values from the accumulated array.
( StackBlitz )
Example 4: Accumulating http responses over time
( StackBlitz )
Related Recipes
Additional Resources
scan ๐ฐ - Official docs
Aggregating streams with reduce and scan using RxJS ๐ฅ - Ben Lesh
Updating data with scan ๐ฅ ๐ต - John Linquist
Transformation operator: scan ๐ฅ ๐ต - Andrรฉ Staltz
Build your own scan operator ๐ฅ - Kwinten Pisman
๐ Source Code: https://github.com/ReactiveX/rxjs/blob/master/src/internal/operators/scan.ts
Last updated