代码如下:
如果在distinct()运算符之前注入do(),则一切正常
很好,但是由于某些原因,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);发布于 2016-08-24 07:37:52
根据reactivex.io提供的函数描述,我的理解是.distinct()操作符实际上应该以您编写的方式工作(无论这是不是一个bug,我不确定)。
但是,由于您的代码不起作用(正如我已经测试过的),另一种选择是在调用.distinct()之前使用.pluck()提取值。在我的测试中,这将按预期工作(不向.distinct()调用提供任何参数)。
基于您的代码的示例:
.do(logToConsole) // DEBUG: all objects are logged to the console
.pluck("code")
.distinct() // now works as expected
.subscribe(logToConsole);`https://stackoverflow.com/questions/37159519
复制相似问题