将多个可观察对象提取到已映射的流中 angular rxjs

Extracting multiple observables into an already mapped stream angular rxjs

目前正在 Firebase 中构建一个项目 Angular 4. 我正在拉入一个数据对象,其中包含许多数据键和许多不同的 uid 键。

为了使数据在某种程度上易于管理,我不想将对象数据保存在一些可以在主机上更改的键中。

在我存储了 uid 的实例中,我想为提取的可观察对象创建一个新键。我可以轻松地将它添加为映射对象内的可观察对象,但我想在流期间实际将可观察对象提取到对象中。这是我目前拥有的:

 this.ss.data()
  .map(res => {
    res['test1'] = this.fb.serie(res['serieUid']).map(res => res)
    return res
  })
  .subscribe(res => {
    console.log(res)
  })

  Result -
    serieUid:"-Kk1FeElvKMFcyrhQP4T"
    state:"Tennessee"
    test1:FirebaseObjectObservable
    timezone:"US/Eastern"

但我想在 'test1' 中提取提取的可观察对象,我已经失败了几个小时,对此感到困惑。 uid 有不止一个实例,所以这必须发生多次。那就订阅吧。

回答如下:

这是我在最终函数中得到的结果

getRound(roundUid: string) {

  /**
    Retrieves a round based simply on the uid
    supplied.
  **/

  return this.fb.round(roundUid)
    .mergeMap(round => // Get Serie Data
      this.fb.serie(round['serieUid']).map(serie => {round['serie'] = this.cs.cleanForFb(serie); return round})
    )
    .mergeMap(round => // Get Type Data
      this.fb.type(round['typeUid']).map(type => {round['type'] = this.cs.cleanForFb(type); return round})
    )
    .mergeMap(round => // Get sx Coast Data
      this.fb.coast(round['sxCoastUid']).map(sxCoast => {round['sxCoast'] = this.cs.cleanForFb(sxCoast); return round})
    )
}

试试这个。 mergeMap() 运算符订阅内部 Observable,其结果使用 map() 变成原来的 res 更新为 test1.

this.ss.data()
  .mergeMap(res => this.fb.serie(res['serieUid'])
    .map(innerRes => {
      res['test1'] = innerRes;
      return res;
    })
  )
  .subscribe(res => {
    console.log(res)
  })