2016-12-05 38 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。