What is the RxJS equivalent of Array reduce? What if I want to emit my reduced or aggregated value at each event? This brief tutorial covers Observable operators reduce() and scan(), their differences and gotchas.

In ES5, the Array's reduce function works like this:

var ary = [0,1,2,3,4];

var res = ary.reduce(function(preVal, item){
return preVal+item ;
}, 0); console.log(res); //

In RxJS, also has reduce function:

It gives the same result.

var source = Rx.Observable.fromArray([0,1,2,3,4]);

source.reduce(function(preVal, item){
return preVal+item ;
}, 0).subscribe(function(res){
console.clear();
console.log(res);
});

Let's do it async:

We will wait for 2.5 seconds until it gives result "10". This means the reduce() function in RxJS, it will not exec until the observable finihsed.

var source = Rx.Observable.interval(500).take(5);

source.reduce(function(preVal, item){
return preVal+item ;
}, 0).subscribe(function(res){
console.clear();
console.log(res);
});

So if we just write:

var source = Rx.Observable.interval(500);

And never finish it, we won't get result by reduce() funtion.

Use sacn() function instead of reduce() to get result each time:

var source = Rx.Observable.interval(500).take(5);

source.scan(0, function(preVal, item){
return preVal+item ;
}).subscribe(function(res){
console.log(res);
}); /*
0
1
3
6
10
*/

when I run this, you'll see each time it ticks in, I get the next value, the next reduced value. One nice difference with scan though is, since it doesn't wait for completion, if I were to run this again, it's actually going to give me a result every time.

[RxJS] Aggregating Streams With Reduce And Scan using RxJS的更多相关文章

  1. [RxJS] Combining streams in RxJS

    Source: Link We will looking some opreators for combining stream in RxJS: merge combineLatest withLa ...

  2. [RxJS] Sharing Streams with Share

    A stream will run with each new subscription added to it. This lesson shows the benefits of using sh ...

  3. [RxJS] Combining Streams with CombineLatest

    Two streams often need to work together to produce the values you’ll need. This lesson shows how to ...

  4. [RxJS] Toggle A Stream On And Off With RxJS

    This lesson covers how to toggle an observable on and off from another observable by showing how to ...

  5. [RxJS] Reactive Programming - Clear data while loading with RxJS startWith()

    In currently implemention, there is one problem, when the page load and click refresh button, the us ...

  6. [RxJS] Reactive Programming - Rendering on the DOM with RxJS

    <!DOCTYPE html> <html> <head> <script src="https://code.jquery.com/jquery- ...

  7. [RxJS] Implement the `map` Operator from Scratch in RxJS

    While it's great to use the RxJS built-in operators, it's also important to realize you now have the ...

  8. [RxJS] Create a Reusable Operator from Scratch in RxJS

    With knowledge of extending Subscriber and using source.lift to connect a source to a subscriber, yo ...

  9. [RxJS] Reactive Programming - Using cached network data with RxJS -- withLatestFrom()

    So now we want to replace one user when we click the 'x' button. To do that, we want: 1. Get the cac ...

随机推荐

  1. CI源码引用使用--php引用demo,静态变量和引用关系

    CI源码引用使用在Common.php中,加载配置和类的方法 function &test() {     static $a = '';     if (!$a) {         $a ...

  2. PHP文件上传与安全

    文件上传的流程 上传必须由POST方式的file类型表单提交,被提交的地方 一定是一个php程序,用户在表单使用file类型的域.选在一个自己电脑上的文件,提交到php程序以后 其实就已经完成了一个上 ...

  3. 编程思想—面向切面编程(AOP)

    谈到面向切面的编程,我们很容易关联到面向对象编程(OOP).个人对这两种编程方式的解释为:两种编程思想只是站在编程的角度问题. OOP注重的是对象,怎么对对象行为和方法的抽象.如何封装一个具有完整属性 ...

  4. hustoj 1017 - Exact cover dancing link

    1017 - Exact cover Time Limit: 15s Memory Limit: 128MB Special Judge Submissions: 5851 Solved: 3092 ...

  5. Contest 20140923 潛行世界 拓撲排序,期望

    潜行世界 查看 提交 统计 提问 总时间限制:  10000ms 内存限制:  256000kB 描述 HJA和学弟还在旅游中,这次他们来到了潜行世界.潜行世界是一个N个点M条边的有向无环图.每条路对 ...

  6. 对话机器学习大神Yoshua Bengio(下)

    对话机器学习大神Yoshua Bengio(下) Yoshua Bengio教授(个人主页)是机器学习大神之一,尤其是在深度学习这个领域.他连同Geoff Hinton老先生以及 Yann LeCun ...

  7. 自己总结的一些android公共库

    本文主要介绍自己在android开发中总结的一些公共库,目前包括下拉刷新ListView.可以响应各个方向CompoundDrawables点击操作的TextView.图片缓存,不断更新,欢迎交流 ? ...

  8. java 类的加载,链接,初始化

    本篇的话题,讨论Java类的加载.链接和初始化.Java字节代码的表现形式是字节数组(byte[]),而Java类在JVM中的表现形式是java.lang.Class类的对象.一个Java类从字节代码 ...

  9. vim下如何删除某行之后的所有行

    使用dG进行删除 在命令模式下将光标置于要删除的起始行,然后依次输入d,G

  10. Trailing return types

    Trailing return types是C++11关于函数声明的语言特性之一,旨在解决模版编程遇到的语法相关的问题,先看一个简单例子,感受一下什么是trailing return types: C ...