简体   繁体   English

Rxjs:map 数组字段中的每个元素来自 object 的可观察对象和另一个可观察对象

[英]Rxjs: map each element in an array field from an observable of object with another observable

I have a method called getTransactionDetails(id:string), and it returns an Observable of TransactionDetails object我有一个名为 getTransactionDetails(id:string) 的方法,它返回一个 Observable of TransactionDetails object

getTransactionDetails(id:string):Observable<TransactionDetails> 

The TransactionDetails object looks like this: TransactionDetails object 如下所示:

class TranscationDetails{
    id:string;
    accessCode: string;
    veggiesList: Array<Veggie>;
    meatList: Array<Meat>;
}

The Veggie object looks like this: Veggie object 看起来像这样:

class Veggie{
    id: string;
    name: string;
}

I have another method called getVeggieSummary(veggieId: string), and it returns an Observable of VeggieSummary:我有另一个方法叫做 getVeggieSummary(veggieId: string),它返回一个 VeggieSummary 的 Observable:

getVeggieSummary(veggiesId:string):Observable<VeggieSummary>

VeggieSummary looks like this VeggieSummary 看起来像这样

class VeggieSummary{
    id:string;
    name:string;
    color:string;
    kind:string;
}

I would like to call getTransactionDetails(1), and transform all the Veggies elements in the veggiesList to be VeggieSummary and return an VeggieSummary array.我想调用 getTransactionDetails(1),并将 veggiesList 中的所有 Veggies 元素转换为 VeggieSummary 并返回一个 VeggieSummary 数组。

This is what I have这就是我所拥有的

getTransactionDetails(1)
.flatMap(transactionDetails=>
    transactionDetails.veggiesList
       .map(veggie=>getVeggieSummary(veggie.id)))
.subscribe(result=>
    console.log(result)
)

Currently, result is an Observable<VeggieSummary> and the inner observable is not even getting subscribed...目前, result是一个Observable<VeggieSummary>并且内部 observable 甚至没有被订阅......

How can I have an Observable<VeggieSummary[]> with rxjs operators?我怎样才能拥有一个带有 rxjs 运算符的Observable<VeggieSummary[]>

note: My app's angular version does not support pipe. The RxJS version is v5注意:我的app的angular版本不支持pipe,RxJS版本是v5

Could you try wrapping the array with forkjoin to see if it fixes the issue?您能否尝试使用forkjoin包装数组以查看它是否解决了问题? I think currently it is returning Observable<VeggieSummary>[]我认为目前它正在返回Observable<VeggieSummary>[]

getTransactionDetails(1)
.flatMap(transactionDetails=>
    forkJoin(transactionDetails.veggiesList
       .map(veggie=>getVeggieSummary(veggie.id))))
.subscribe(result=>
    console.log(result)
)

You just need to wrap the inner observable with forkJoin , this converts the Observable<VeggieSummary>[] to Observable<VeggieSummary[]>您只需要用forkJoin包装内部可观察对象,这会将Observable<VeggieSummary>[]转换为Observable<VeggieSummary[]>

    getTransactionDetails(1)
      .flatMap((transactionDetails) =>
        forkJoin(
          transactionDetails.veggiesList.map((veggie) =>
            getVeggieSummary(veggie.id)
          )
        )
      )
      .subscribe((result) => console.log(result));

with forkjoin, this works:使用forkjoin,这有效:

  getTransactionDetails(1)
  .flatMap((transactionDetails) =>
    forkJoin(
      transactionDetails.veggiesList.map((veggie) =>
        getVeggieSummary(veggie.id)
      )
    )
  )
  .subscribe((result) => console.log(result));

I found zip with the spread operator (...) also works:我发现 zip 与传播运算符 (...) 也有效:

   getTransactionDetails(1)
  .flatMap((transactionDetails) =>
    zip(
      ...transactionDetails.veggiesList.map((veggie) =>
        getVeggieSummary(veggie.id)
      )
    )
  )
  .subscribe((result) => console.log(result));

I learned that from another post.我从另一篇文章中了解到的。

But I am not sure which one to use, forkjoin or zip?但我不确定使用哪个,forkjoin 还是 zip? Does it matter?有关系吗?

I also learned about rest operator/spread operator (...) from that post.我还从该帖子中了解了 rest 运算符/展开运算符 (...)。

But I still don't quite understand fully how it works in this example.但是我仍然不太了解它在这个例子中是如何工作的。

I am not sure which one I am using, am I using the rest or spread operator?我不确定我使用的是哪个,我使用的是 rest 还是传播运算符?

and what is it equivalent?它等价于什么?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM