如何等待多个结果,然后根据接收到的数据过滤它们?

125*_*748 5 javascript observable rxjs ngrx angular

我有两个 observables,我想等待两者的结果,以便我可以根据另一个过滤一个的结果。他们各自工作:

this.allValues$ = this.store.select(selectors.getAllValues)
this.myId$ = this.store.select(selectors.myId)
Run Code Online (Sandbox Code Playgroud)

我可以使用异步管道将它们渲染到模板

但是我想创建一个包含过滤数组的类属性。如果是同步 JS,则类似于

this.filteredResults = allValues.filter(value => value.id === myId)
Run Code Online (Sandbox Code Playgroud)

ziping 会让我得到值

this.filteredResults$ = zip(
  this.store.select(selectors.getAllValues),
  this.store.select(selectors.myId)
)
Run Code Online (Sandbox Code Playgroud)

模板:结果:{{filteredResults$ | 异步 | json }}

但我无法理解如何像我想要的那样过滤。我试过将 a 链接pipezip

.pipe(
   tap((...args) => {
     console.log({ args }) // only one result so no hope of dropping in `map` or `filter` here
   })
)
Run Code Online (Sandbox Code Playgroud)

但这具有allValues从结果集中删除数组的效果。allValues大得多,所以大概需要更长的时间并且 zip 不再等待所有东西发出,所以我想pipe这不是解决方案,尽管它看起来很接近。

我怎样才能访问这两个结果集,过滤它们,并将结果放入一个可观察的对象中,我可以在模板中呈现filteredResults$ | async | json

Mil*_*lad 0

您可以使用concatMapswitchMap

this.filteredResults = this.store.select(selectors.myId).pipe(
  concatMap(myId => {
    return this.store
      .select(selectors.getAllValues)
      .pipe(filter(value => value.id === myId));
  })
);
Run Code Online (Sandbox Code Playgroud)