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. CURL传输与获取功能

    什么是CURL? 利用URL语法爱命令行方式下工作的文件传输工具.它支持很多协议.它支持认证功能.php中常用都实现更复杂的传输功能. 实现的功能: 1.实现远程获取和采集内容 2.实现PHP 网页版 ...

  2. 配置安装theano环境(非GPU版)

    终于成功配置了theano环境,但由于本机没有gpu,所以配置的是非gpu版本的theano,下面将具体过程进行描述(安装成功后,有时对python的各种库进行更新时,可能会导致某个模块无法调用其他被 ...

  3. [BZOJ 1086] [SCOI2005] 王室联邦 【树分块】

    题目链接:BZOJ - 1086 题目分析 这道题要求给树分块,使得每一块的大小在 [B, 3B] 之间,并且可以通过一个块外的节点(块根)使得整个块联通. 那么我们使用一种 DFS,维护一个栈,DF ...

  4. Stanford Parser学习入门(1)-Eclipse中配置

    Stanford Parser是斯坦福大学研发的用于语法分析的工具,属于stanford nlp系列工具之一.本文主要介绍Standfor Parser的入门用法. 在Stanford官方网站下载最新 ...

  5. SolrCloud阶段总结

    http://www.cnblogs.com/guozk/p/3498844.html SolrCloud阶段总结 开发类型 全文检索相关开发 Solr版本 4.2 文件内容 本文介绍SolrClou ...

  6. struts2不能通过ONGL方式取出request中的Atrribute

    请看下面一个很简单的Action package com.ahgw.main.action; import org.springframework.stereotype.Controller; /** ...

  7. Android 一个页面上下两个ListView的页面显示

    Android 一个页面上下两个ListView,当上面的ListView过长时,下面的List基本没有了滑动空间,查阅网上资料,解决办法基本是采用ScrollView做页面滑动,notifyData ...

  8. (转载)Android开发者必知的开发资源

    (转载)http://www.importnew.com/3988.html 随着Android平台市场份额的持续猛增 ,越来越多的开发者开始投入Android应用程序的开发大潮.如果您是一位2013 ...

  9. Android问题:设置了requestWindowfeature(window.feature_no_title)后,为什么还要getwindow.setFlags?

    //设置窗体全屏getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams. ...

  10. 谷歌浏览器怎么调试js

    首先我们打开开发者工具,你可以直接在页面上点击右键,然后选择审查元素或者在Chrome的工具中找到或者你直接记住这个快捷方式: Ctrl+Shift+I (或者Ctrl+Shift+J直接打开控制台) ...