2016-12-05 16 views
1

我有这些对象的obeservables:角2个Rxjs:上的对象应用不同

{ 
     id : "f3055770-6e66-4936-8e9a-732b53121549" 
     message:"Empty Notification for test ......" 
     navigationLink:"/fusion/home" 
     seen:false 
     sendedOn:"2016-12-02T15:19:44.856Z" 
     userId :null 
     } 

我想不接收复制对象(基于IDS)和i使用这种方法来实现它

notify(userID: string) { 
    return Observable.interval(5000) 
     .map(() => this.baseUrl + '/notification/GetUnseen?userId=' + userID) 
     .switchMap(url => { 
      return Observable.from(this.datatService.get(url)); 
     }) 
     .flatMap(response => Observable.from(response.json().slice())) 

} 

当我添加distinct(x => x.id)作为最后一个操作符我只有一个对象而不是四个帮助?

UPDATE:

我打电话,我组分的OnInit()生命周期中的这一方法,使该方法执行每5秒钟得到通知,我用DISTICT这样:

notify(userID: string) { 
    return Observable.interval(5000) 
     .map(() => this.baseUrl + '/notification/GetUnseen?userId=' + userID) 
     .switchMap(url => { 
      return Observable.from(this.datatService.get(url)); 
     }) 
     .flatMap(response => Observable.from(response.json().slice())) 
     .distinct(x => x.id); 
} 

UPDATE 2

Serveur原始响应:

"[{"userId":null,"sendedOn":"2016-12-02T15:19:44.856Z","message":"Empty Notification for test ......","navigationLink":"/fusion/home","seen":false,"id":"f3055770-6e66-4936-8e9a-732b53121549"},{"userId":null,"sendedOn":"2016-12-02T15:19:45.146Z","message":"Empty Notification for test ......","navigationLink":"/fusion/home","seen":false,"id":"ce172122-11d9-4054-a3e4-594c8c910a7d"},{"userId":null,"sendedOn":"2016-12-02T15:19:45.146Z","message":"Empty Notification for test ......","navigationLink":"/fusion/home","seen":false,"id":"66e32c45-f544-4ce6-901c-e5ac64904954"},{"userId":null,"sendedOn":"2016-12-02T15:19:45.147Z","message":"Empty Notification for test ......","navigationLink":"/fusion/home","seen":false,"id":"4c2322cb-526c-490e-8a86-f1e9ced1c34f"}]" 
+0

您可以请给出上下文:1)你怎么称呼这个方法? 2)你在哪里添加'distinct'运算符? – Meir

+0

你确定这四个对象没有相同的'id'吗? – martin

+0

更新,@马丁是啊,我舒舒服服,是不一样的 –

回答

1

我找到了独特的解决方案!

事实上,传递给disctinct功能可以采取2个参数(precedentValue,actualValue),这样就可以解决这个问题是这样的:

.... 
.... 
.distinct(function (x, y) { return x.id === y.id; }); 

它返回每个迭代一个布尔值(是x.id = y.id?=> true或false ...)。

**更新**

我的错误是,我一直在寻找的rxjs 4.0文档,我的项目是rxjs 5。