码迷,mamicode.com
首页 > Web开发 > 详细

[RxJS] Filtering operators: distinct and distinctUntilChanged

时间:2016-05-30 21:42:50      阅读:163      评论:0      收藏:0      [点我收藏+]

标签:

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

标签:

原文地址:http://www.cnblogs.com/Answer1215/p/5543920.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!