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的更多相关文章

  1. [RxJS] Filtering operators: take, first, skip

    There are more operators in the filtering category besides filter(). This lesson will teach how take ...

  2. [RxJS] Filtering operators: takeLast, last

    Operators take(), skip(), and first() all refer to values emitted in the beginning of an Observable ...

  3. [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 ...

  4. [RxJS] Filtering operators: skipWhile and skipUntil

    After takeUntil() and takeWhile() function, let's have a look on skipWhile() and skilUntil() functio ...

  5. [RxJS] Filtering operators: takeUntil, takeWhile

    take(), takeLast(), first(), last(), those opreators all take number or no param. takeUtil and takeW ...

  6. Rxjs常用operators

    本文使用的是angular6内置的rxjs,版本号为6.3.3 concat 通过顺序地发出多个 Observables 的值将它们连接起来,一个接一个的. 参数: 名称 类型 属性 描述 other ...

  7. [RxJS] Transformation operators: debounce and debounceTime

    Debounce and debounceTime operators are similar to delayWhen and delay, with the difference that the ...

  8. [RxJS] Transformation operators: delay and delayWhen

    This lessons teaches about delay and delayWhen: simple operators that time shift. delay(number | dat ...

  9. [RxJS] Creation operators: interval and timer

    It is quite common to need an Observable that ticks periodically, for instance every second or every ...

随机推荐

  1. include()、include_once()与require()、require_once()的异同点

    相同点: 首先include().include_once()与require().require_once()都是用来包含并运行指定文件的,并且包含的文件在执行时在结构上是完全一样的. 例如:inc ...

  2. Android 安全概述

    1. 保密: 信息.文档加密.解密 2. 鉴别.认证:就像确认与我们打电话的确实是XXX,而不是骗子 3. 完整性:信息不能被篡改, 4. 不可否认性:确定信息有who产生,并且将来不能否认

  3. paramiko学习

    1. ssh简介 2. ssh私有key/共有key的区别 3. paramiko的常规使用

  4. 小爬虫。爬取网站多页的通知标题并存取在txt文档里。

    爬取网页中通知标题的内容展示: this is  1  page!<精算学综合>科目考试参考大纲2016年上半年研究生开题报告评议审核结果公示[答辩]2016下半年研究生论文答辩及学位评定 ...

  5. ListView的setOnItemClickListener和setOnItemLongClickListener同时响应的问题

    lvContentList.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(A ...

  6. 练习2 E题 - 求奇数的乘积

      Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u   Description 给你n ...

  7. 典当行以及海尔java小节

    1.视图问题,发现jar包都出现在根目录下面了,非常不方便.结果如下表: 原始视图是JavaEE,切换到Java视图即可: 2.Tomcat编译的时候什么都没有加载,看到的是一堆红字,那是因为tomc ...

  8. [BZOJ 2594] [Wc2006]水管局长数据加强版 【LCT】

    题目链接:BZOJ - 2594 题目分析 这道题如果没有删边的操作,那么就是 NOIP2013 货车运输,求两点之间的一条路径,使得边权最大的边的边权尽量小. 那么,这条路径就是最小生成树上这两点之 ...

  9. android的原理,为什么不需要手动关闭程序

    转自android的原理,为什么不需要手动关闭程序 不用在意剩余内存的大小,其实很多人都是把使用其他系统的习惯带过来来了. Andoird大多应用没有退出的设计其实是有道理的,这和系统对进程的调度机制 ...

  10. 最牛「CSRF防护」,带你进入大虾们的圈子!

    简单理解 CSRF 什么是 CSRF? CSRF,通常称为跨站请求伪造,英文名 Cross-site request forgery 缩写 CSRF,是一种对网站的恶意攻击.一个跨站请求伪造攻击迫使登 ...