Simple filter on array of RXJS Observable

后端 未结 4 852
独厮守ぢ
独厮守ぢ 2020-12-12 19:21

I am starting my project with Angular2 and the developers seem to recommend RXJS Observable instead of Promises.

I have achieved to retrieve a list of elements (epic

4条回答
  •  北荒
    北荒 (楼主)
    2020-12-12 19:34

    You'll want to filter the actual array and not the observable wrapped around it. So you'll map the content of the Observable (which is an Epic[]) to a filtered Epic.

    getEpic(id: string): Observable {
      return this.getEpics()
         .map(epics => epics.filter(epic => epic.id === id)[0]);
    }
    

    Then afterwards you can subscribe to getEpic and do whatever you want with it.

提交回复
热议问题