嵌套的RXJS映射操作员的目的是什么?

发布于 2025-02-03 08:50:44 字数 485 浏览 3 评论 0原文

我目前正在进行在线角度课程,我意识到RXJS在Web开发中广泛使用。我正在尝试,遇到了一个嵌套的地图操作员,我可以理解正在发生的事情,但我似乎无法在上面找到很多信息。

    this.productService.getProducts().pipe(
      map(products => 
        products.map(product => {
          const data = {...product.payload.val() as Product}; 
          return data;
        }))).subscribe(p => this.products = p)

因此,从我的理解中,GetProducts()函数返回可观察到的列表,然后外部地图运算符会发出可观察到的单个可观察到的内部地图运算符,然后我可以从HTTP响应中访问有效载荷数据?如果这是正确的,是否有一种更轻松/更清洁的方法来执行此功能?

I am currently doing an online Angular course and I realized that RxJS is quite broadly used in web development. I was experimenting and I came across a nested map operator and I can kind of make sense of what is happening but I cant seem to find much information on it.

    this.productService.getProducts().pipe(
      map(products => 
        products.map(product => {
          const data = {...product.payload.val() as Product}; 
          return data;
        }))).subscribe(p => this.products = p)

So from my understanding is that the getProducts() function returns a list of Observables, then the outer map operator would emit an individual Observable into the inner map operator which then allows me to access the payload data from the http response? If this is correct, isnt there an easier/cleaner way of performing this functionality ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

安人多梦 2025-02-10 08:50:44

当您开始时,RXJS是一次很棒的冒险。

我从代码中的猜测是GetProducts()返回可观察到的可观察到的产品,该可观察到存储在产品中的产品

也可以通过:

this.productService.getProducts().pipe(
      map(products => {...product.payload.val() as Product[]})
)

https:// https://stackblitz.com/editit/dyit/typescript/typescript/typescript/typescript -3jed7f?file = index.ts

RxJS is quite a wonderful adventure when you start with it.

my guess from the code is the getProducts() returns an Observable that emits an Array of Products that are stored in product.payload.val

The map function then transforms the payload from GetProducts() to a typed array of Products.

It could also be done via:

this.productService.getProducts().pipe(
      map(products => {...product.payload.val() as Product[]})
)

https://stackblitz.com/edit/typescript-3jed7f?file=index.ts

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文