Rxjs - Operators - Scan

Signature

export declare function scan<T, R>(
accumulator: (acc: R, value: T) => R,
seed?: T | R): Observable<R>;

把一组值变成一个值,并且每次产生新的聚合值,就会传递出去,不会像reduce那样等最后结果
但初始值是不发出去的

It’s like reduce, but emits the current accumulation whenever the source emits a value.

Parameters

一个聚合方法 聚合值和当前值
一个聚合值的初始值

Sample

直接写上次分页服务里的改进版本吧

var clicks = Rx.Observable.fromEvent(document, 'click');
var ones = clicks.mapTo(1);
var seed = 0;
var count = ones.scan((acc, one) => acc + one, seed);
count.subscribe(x => console.log(x));