2016-12-05 12 views
1

の適用明確な:角度2 Rxjs:私は、これらのオブジェクトのobeservablesを持つオブジェクト

{ 
     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 
     } 

私は(IDに基づいて)重複したオブジェクトを受け取り、私はそれを

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())) 

} 
を達成するために、このメソッドを使用しないしたいと思います

は、私が代わりに任意のヘルプ四のオブジェクトを1つだけ持って、最後の作業として、明確な(X => x.id)を追加すると?

UPDATE:メソッドは、通知を得るために、すべての5秒を実行するよう

は私がdistictこの方法を使用して、私のコンポーネントののOnInit()のライフサイクルでは、このメソッドを呼び出します。

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

これらの4つのオブジェクトに同じ「id」がないことは確かですか? IDが同じ – martin

+0

更新、@martinええ午前SHURE? –

答えて

1

私が明確なために解決策を見つけました!

実際には、disctinctに渡された関数は、2つのパラメータ(precedentValue、実際の値)を取ることができますので、あなたがこのようにこの問題を解決することができます:それは、各反復でブール値を返す

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

を(x.id =ですy.id?=> trueまたはfalse ...)。

** UPDATE **

私のミスは私がrxjs 4.0のドキュメントを探していたということで、私のプロジェクトはrxjs 5です。

関連する問題