RxJS 用另一个 Observable 中的值过滤一个 observable

Pau*_*ard 1 rxjs ngrx angular

我正在寻找用另一个 Observable 中保存的值来过滤 Observable 的最佳 RxJs 方法。

首先,我有一个包含来自路径 Params 的代码的可观察对象。假设它的股票代码为“BTC”$

然后我有一个可观察的商店,它返回 fullCoinList$ 中的硬币列表(其中 1414 个)

我希望 this.coin$ observable 仅包含 this.fullCoinList$ observable 中的项目,该项目在股票名称中的某处包含字符串“BTC”。

FullCoinList$ 看起来像这样。

  [{ticker: "ETHBTC", price: "0.04597600"}
   {ticker: "LTCBTC", price: "0.00457100"}
   {ticker: "BNBBTC", price: "0.01008450"}
   {ticker: "NEOBTC", price: "0.00163300"}
   {ticker: "QTUMETH", price: "0.00541200"}
   {ticker: "EOSETH", price: "0.00229400
   .... + 1408more]
Run Code Online (Sandbox Code Playgroud)

我的 NgOnInit 看起来像这样

ngOnInit(): void {
    this.activatedRoute.paramMap.subscribe( (params: ParamMap) => {
      this.ticker$ = of(params.get('ticker'))
    })

    this.fullCoinList$ = this.store.pipe(select(selectAllCoins))
    
    this.coins$ = this.fullCoinList$.pipe(
      filter( coin => coin.ticker.includes(this.ticker$)) // this line needs work
    )
  }
Run Code Online (Sandbox Code Playgroud)

对于 mergeMap、concatMap 或类似的东西来说,这是一个很好的用例吗?我该如何最好地实施它?我也不确定 include 是正确的方法。

编辑:我添加了 stackBlitz Blitz

Ali*_*F50 6

我会combineLatest结合两个可观察的结果并从那里开始。

import { combineLatest } from 'rxjs';
import { map } from 'rxjs/operators';
...
ngOnInit(): void {
    // we can get rid of a subscription here and assign it to the observable directly
    this.ticker$ = this.activatedRoute.paramMap.pipe(
      map(paramMap => paramMap.get('ticker')),
    );

    this.fullCoinList$ = this.store.pipe(select(selectAllCoins))
    
    this.coins$ = combineLatest(this.fullCoinList$, this.ticker$).pipe(
      // the filter is the array filter and not rxjs filter
      map(([fullCoinList, ticker]) => fullCoinList.filter(fullCoin => fullCoin.ticker.includes(ticker))),
    );
  }
Run Code Online (Sandbox Code Playgroud)

类似的东西应该有效。