Miguel Moura
Miguel Moura

Reputation: 39394

Pass variables to second Map in RXJS

I have the following:

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

How to in first Map get the basket and product with given basketId and productId?

I know how to get them ... Something like:

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

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

But how to pass and be able to use both in second map:

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

Upvotes: 0

Views: 317

Answers (1)

Max K
Max K

Reputation: 1121

You were almost there:

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*/)
    )

Of course they might be undefined in the second map if the ids were not found.

Upvotes: 2

Related Questions