将变量传递给RXJS中的第二个Map

时间:2020-02-19 17:45:53

标签: typescript rxjs

我有以下内容:

zip(this.baskets$.asObservable(), this.products$.asObservable()).
pipe(
  map(([baskets, products]) => /* get basket and product */),
  map(([basket, product]) => /* use basket and product in condition */)
);

在给定的Mapbasket的情况下,如何首先product获得basketIdproductId

我知道如何获得它们……类似

let basket = baskets.find(x => x.id == data.basketId);

let product = products.find(x => x.id == data.productId); 

但是如何在第二个map中通过和使用两者:

map(([basket, product]) => /* use basket and product in condition */)

1 个答案:

答案 0 :(得分:2)

您快到了:

zip(this.baskets$,this.products$).pipe(
      map(([baskets,products]) => {
        const basket = baskets.find(bskt => bskt.id === this.data.basketId);
        const product = products.find(prdct => prdct.id === this.data.productId);
        return [basket,product];
      }),
      map(([basket,product]) => /*now use them here*/)
    )

当然,如果找不到ID,则可能在第二张地图中未定义它们。