[RxJS] Aggregating Streams With Reduce And Scan using RxJS
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的更多相关文章
- [RxJS] Combining streams in RxJS
Source: Link We will looking some opreators for combining stream in RxJS: merge combineLatest withLa ...
- [RxJS] Sharing Streams with Share
A stream will run with each new subscription added to it. This lesson shows the benefits of using sh ...
- [RxJS] Combining Streams with CombineLatest
Two streams often need to work together to produce the values you’ll need. This lesson shows how to ...
- [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 ...
- [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 ...
- [RxJS] Reactive Programming - Rendering on the DOM with RxJS
<!DOCTYPE html> <html> <head> <script src="https://code.jquery.com/jquery- ...
- [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 ...
- [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 ...
- [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 ...
随机推荐
- Python之简单的SMTP发送邮件详细教程附代码
简介 Python发送邮件的教程本人在网站搜索的时候搜索出来了一大堆,但是都是说了一大堆原理然后就推出了实现代码,我测试用给出的代码进行发送邮件时都不成功,后面找了很久才找到原因,这都是没有一个详 ...
- IOS 上传头像-b
感谢大神分享 1.首先,后台给了我这样的接口 1-后台数据接口 2.首先加上代理方法 <UIActionSheetDelegate,UINavigationControllerDelegate, ...
- bzoj 3669: [Noi2014]魔法森林 动态树
3669: [Noi2014]魔法森林 Time Limit: 30 Sec Memory Limit: 512 MBSubmit: 363 Solved: 202[Submit][Status] ...
- [BZOJ 2049] [Sdoi2008] Cave 洞穴勘测 【LCT】
题目链接:BZOJ - 2049 题目分析 LCT的基本模型,包括 Link ,Cut 操作和判断两个点是否在同一棵树内. Link(x, y) : Make_Root(x); Splay(x); F ...
- iReport中求和的问题
数据库取出值TAX_AMT,但是不想在数据库里面计算,太麻烦,后面group by 字段太多.那就放到ireport里面去计算咯 在字段的如下位置进行计算吧.
- 【技术贴】解决myeclipse SVN 提交代码 commit:remains in tree-c
[技术贴]解决myeclipse SVN 提交代码 commit:remains in tree-conflict错误的解决办法 错误是:Aborting commit: xxxxx' remains ...
- The state of binary data in the browser
The state of binary data in the browser Or: "So you wanna store a Blob, huh?" TL;DR Don't ...
- 【BZOJ2434-[Noi2011]】阿狸的打字机(AC自动机(fail树)+离线+树状数组)
Description 阿狸喜欢收藏各种稀奇古怪的东西,最近他淘到一台老式的打字机.打字机上只有28个按键,分别印有26个小写英文字母和'B'.'P'两个字母. 经阿狸研究发现,这个打字机是这样工作的 ...
- Ubuntu下su:authentication failure的解决办法
$ su - rootPassword: su: Authentication failureSorry. 这时候输入 $ sudo passwd rootEnter new UNIX passwor ...
- Astyle:代码格式化工具简明指南
astyle是一个我自己常用的开放源码工具.它可以方便的将程序代码格式化成自己想要的样式而不必人工修改.本来嘛,作为高等生物应该优先去做一些智慧的事情,而不是把时间消耗在机器可以完美完成的事情上. 想 ...