March 2018
Intermediate to advanced
364 pages
8h 21m
English
There used to be an operator called sum(), but it hasn't existed for several versions. What there is instead is .reduce(). With the reduce() operator, we can easily achieve the same thing. The following is how you would write a sum() operator using reduce():
// mathematical/sum.jslet stream = Rx.Observable.of(1, 2, 3, 4) .reduce((acc, curr) => acc + curr);// emits 10stream.subscribe(data => console.log(data));
What this does is to loop through all the emitted values and sum up the results. So, in essence, it sums up everything. Of course, this kind of operator can not only be applied to numbers, but to objects as well. The difference lies in how you carry out the reduce() operation. The following example covers such a scenario:
let stream ...
Read now
Unlock full access