[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 ...
随机推荐
- 制作font-icon有感
连日来有些空闲,趁着这闲余时间,我尝试亲自制作一些Font-Icon,让以后可以运用到工作中.但是基于本人水平有限,PS操作只能以非常基础来形容,而AI呢,根本就只会放大操作.在这过程真的非常感谢设计 ...
- nginx 基础文档
Nginx基础 1. nginx安装 2. nginx 编译参数详解 3. nginx安装配置+清缓存模块安装 4. nginx+PHP 5.5 5. nginx配置虚拟主机 6. ngi ...
- js一些方法的扩展
//JS扩展方法与C#的扩展方法非常相似,也是可以链式调用的,也是通过对某个类的扩展写法来实现.这个东西非常好用,如果将预先写好的方法放到一个js里面引用的话,那么后面写js将非常有趣. //下面给出 ...
- GBin1插件推荐之马可波罗(Marco Polo),jQuery的自动补齐插件 - Autocomplete Plugin
让我们Google一下"jQuery autocomplete plugin"(jquery自动补齐插件).在过去的4年中,我已经Google了很多次这个组合了.然而结果并没有变化 ...
- 【技术贴】解决myeclipse SVN 提交代码 commit:remains in tree-c
[技术贴]解决myeclipse SVN 提交代码 commit:remains in tree-conflict错误的解决办法 错误是:Aborting commit: xxxxx' remains ...
- 框架中的HTML DOM Event 对象
js中的this上下文会因事件而转换成html dom对象. 所以就有这样获取当前触发事件的dom对象: window.event.srcElement || window.event.target; ...
- Spring REST
前面介绍过Spring的MVC结合不同的view显示不同的数据,如:结合json的 view显示json.结合xml的view显示xml文档.那么这些数据除了在WebBrowser中用JavaScri ...
- mysql之索引方面的知识点总结
索引的类型: 普通索引:这是最基本的索引类型,没唯一性之类的限制. 唯一性索引:和普通索引基本相同,但所有的索引列只能出现一次,保持唯一性. 主键:主键是一种唯一索引,但必须指定为"PRIM ...
- python装饰器之使用情景分析
http://blog.csdn.net/yueguanghaidao/article/details/10089181
- poj1637
混合图欧拉回路首先先明确基本概念连通的无向图存在欧拉回路当且仅当不存在奇点连通的有向图当且仅当每个点入度=出度这道题我们显然应该当作连通的有向图来做这个问题的困难之处在于我不知道应该从无向边的什么方向 ...