[RxJS] Filtering operators: distinct and distinctUntilChanged
2016-05-30 21:13
549 查看
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():
distinct(comparFn, flushFn):
With CamperFn():
with FlusherFn:
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" */
相关文章推荐
- angular js知识总结
- JavaScript学习笔记04-函数
- js-DOM总结
- js代码加总结
- javascript闭包
- 3分钟理解JSON
- 如何理解Javscript中的函数调用和"this"
- [RxJS] Filtering operators: throttle and throttleTime
- 点击元素使其在毫秒内从不透明变为透明
- JavaScript JSON 学习笔记
- Package.json中dependencies依赖包中^符号和~符号前缀的区别
- js事件处理函数/监听函数
- (三) 浏览器内置对象及常用方法
- (二) JavaScript内置对象及常用方法
- javascript Function
- Jsoup解析html的基本用法
- 8年javascript知识点积累
- 《JavaScript学习笔记十》:无缝滚动的实现
- 《JavaScript学习笔记九》:延时框的实现
- 《JavaScript学习笔记八》:数字时钟的实现