[RxJS] Filtering operators: distinct and distinctUntilChanged
Operator distinct() and its variants are an important type of Filtering operator. This lessons shows how they work and in what cases are they useful.
distinctUntilChanged():
var foo = Rx.Observable.interval(500).take(5)
.zip(Rx.Observable.of('a','b','a','a','b'), (x,y)=>y); /*
--a--b--a--a--b|
distinctUntilChanged
--a--b--a-----b|
*/ var result = foo.distinctUntilChanged(); result.subscribe(
function (x) { console.log('next ' + x); },
function (err) { console.log('error ' + err); },
function () { console.log('done'); },
);
distinct(comparFn, flushFn):
var foo = Rx.Observable.interval(500).take(5)
.zip(Rx.Observable.of('a','b','a','a','b'), (x,y)=>y); /*
--a--b--a--a--b|
distinct
--a--b---------|
*/ var result = foo.distinct(); result.subscribe(
function (x) { console.log('next ' + x); },
function (err) { console.log('error ' + err); },
function () { console.log('done'); },
); /*
"next a"
"next b"
"done"
*/
With CamperFn():
var foo = Rx.Observable.interval(500).take(5)
.zip(Rx.Observable.of('a','b','a','A','b'), (x,y)=>y); var comparFn = (x, y) => {
return x.toLowerCase() === y.toLowerCase();
} /*
--a--b--a--A--b|
distinct
--a--b---------|
*/ var result = foo.distinct(comparFn); result.subscribe(
function (x) { console.log('next ' + x); },
function (err) { console.log('error ' + err); },
function () { console.log('done'); },
); /*
"next a"
"next b"
"done"
*/
with FlusherFn:
var foo = Rx.Observable.interval(500).take(5)
.zip(Rx.Observable.of('a','b','a','A','b'), (x,y)=>y); var comparFn = (x, y) => {
return x.toLowerCase() === y.toLowerCase();
} var flushFn = Rx.Observable.interval(1100).take(1)
.concat(Rx.Observable.never()); /*
--a--b--a--A--b|
-------0--------
distinct(comparFn, flushFn)
--a--b--a-----b|
*/ var result = foo.distinct(comparFn, flushFn); result.subscribe(
function (x) { console.log('next ' + x); },
function (err) { console.log('error ' + err); },
function () { console.log('done'); },
); /*
"next a"
"next b"
"next a"
"next b"
"done"
*/
[RxJS] Filtering operators: distinct and distinctUntilChanged的更多相关文章
- [RxJS] Filtering operators: take, first, skip
There are more operators in the filtering category besides filter(). This lesson will teach how take ...
- [RxJS] Filtering operators: takeLast, last
Operators take(), skip(), and first() all refer to values emitted in the beginning of an Observable ...
- [RxJS] Filtering operators: throttle and throttleTime
Debounce is known to be a rate-limiting operator, but it's not the only one. This lessons introduces ...
- [RxJS] Filtering operators: skipWhile and skipUntil
After takeUntil() and takeWhile() function, let's have a look on skipWhile() and skilUntil() functio ...
- [RxJS] Filtering operators: takeUntil, takeWhile
take(), takeLast(), first(), last(), those opreators all take number or no param. takeUtil and takeW ...
- Rxjs常用operators
本文使用的是angular6内置的rxjs,版本号为6.3.3 concat 通过顺序地发出多个 Observables 的值将它们连接起来,一个接一个的. 参数: 名称 类型 属性 描述 other ...
- [RxJS] Transformation operators: debounce and debounceTime
Debounce and debounceTime operators are similar to delayWhen and delay, with the difference that the ...
- [RxJS] Transformation operators: delay and delayWhen
This lessons teaches about delay and delayWhen: simple operators that time shift. delay(number | dat ...
- [RxJS] Creation operators: interval and timer
It is quite common to need an Observable that ticks periodically, for instance every second or every ...
随机推荐
- js更新页面,随机更新数字
代码1: <script> function getRandom(){ var i = Math.random()*40+160; document.getElementById(&quo ...
- 使用PHP对文件进行压缩解压(zip)
使用虚拟主机进行文件上传时最常用的工具莫过于FTP了,但是使用FTP有一个弊端就是文件太多时上传或下载速度比较慢,如果上传时将文件打包,上传后在 空间解压缩,同样下载前将文件打包压缩以压缩包的形式下载 ...
- PHP代码分离
所谓的代码分离 其实只是一种思路,既然是一种思路 那就意味着他是有需求的 没有需求就没有解决方案 没有方案就不存在思路. 在这之前,我们制作 PHP 程序页面的时候.都是 HTML 和 PHP 混合写 ...
- Tomcat根目录下work文件夹的作用
今天遇到了这样的一个问题:就是我之前把项目部署到了tomcat下,运行没有任何问题,后来我把我之前的改项目的原项目文件部署上去后,运行时总是出现之前的界面,而服务器又是启动正常的;通过仔细的检查后,发 ...
- [BZOJ 1218] [HNOI2003] 激光炸弹 【n logn 做法 - 扫描线 + 线段树】
题目链接:BZOJ - 1218 题目分析 可以覆盖一个边长为 R 的正方形,但是不能包括边界,所以等价于一个边长为 R - 1 的正方形. 坐标范围 <= 5000 ,直接 n^2 的二维前缀 ...
- 7 Tools for Data Visualization in R, Python, and Julia
7 Tools for Data Visualization in R, Python, and Julia Last week, some examples of creating visualiz ...
- 当用DJANGO的migrate不成功时。。。。
URL:http://my.oschina.net/u/862582/blog/355421 因为操作SQL数据库时不规范,或是多人开发时产生了同步问题,就可能导致正规的MIGRATE时不能完成. 已 ...
- WINDOWS下简单操作SQLITE3
有测试操作的时候,还是很好的说~~~ 找个sqlite3.txt下载 sqlite3.exe db.sqlite3 SQLite version 3.7.13 2012-06-11 02:05:22 ...
- [spring-framework]Spring定时器的配置和使用
开发中我们常常会做一些定时任务,这些任务有开始时间,并会按一定的周期或规则执行.如此我们在Java程序开发中使用定时器来处理定时任务. <!-- MessageRequestTask类中包含了m ...
- SQL servere 范式、事务
一.数据库范式: 1.构造数据库必须遵循一定的规则.在关系数据库中,这种规则就是范式. 范式是符合某一种级别的关系模式的集合.数据库中的关系必须满足一定的要求,即满足不同的范式. 满足最低要求的范式是 ...