简体   繁体   English

Rxjs5,distinct()无法正常工作

[英]Rxjs5, distinct() is not working

This is the code: 这是代码:

if you inject the do() before the distinct() operator everything is working 如果您在distinct()运算符之前注入do(),则一切正常

just fine, but for some reasons distinct() print only the first object 很好,但是由于某些原因,distinct()仅打印第一个对象

_ _

Rx.Observable
  .interval(1000)
  .flatMap(_ => { // JSONP request
    return Rx.Observable.create(observer => {
      window.eqfeed_callback = res => {
        observer.next(res);
        observer.complete();
      };

      loadJSONP(QUAKE_URL);
    }).retry(3);
  })
  .flatMap(res => Rx.Observable.from(res.features))
  .map(quake => {
    return {
      lat: quake.geometry.coordinates[1],
      lng: quake.geometry.coordinates[0],
      size: quake.properties.mag * 10000,
      code: quake.properties.code
    };
  })
  .do(logToConsole) // DEBUG: all objects are logged to the console
  .distinct(quake => quake.code) // it only log the first object !
  .subscribe(logToConsole);

Based on the function description provided by reactivex.io, my understanding is that the .distinct() operator should in fact work the way you have coded (whether this is a bug or not, I am unsure). 根据.distinct()提供的功能描述,我的理解是.distinct()运算符实际上应该按照您的编码方式工作(我不确定这是否是错误)。

However, seeing as your code does not work (as I have tested it also), another option is to use .pluck() to pull out the value before calling .distinct() . 但是,由于您的代码不起作用(正如我已经测试过的那样),另一个选择是使用.pluck()在调用.distinct()之前提取值。 In my testing, this will now work as expected (do not provide any arguments to the .distinct() call). 在我的测试中,这现在将按预期工作(不向.distinct()调用提供任何参数)。

Example based on your code: 根据您的代码的示例:

.do(logToConsole) // DEBUG: all objects are logged to the console
.pluck("code")
.distinct() // now works as expected
.subscribe(logToConsole);`

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM