【问题标题】:Observables combine with combineLatest Observables which are generated in a for loopObservables 与在 for 循环中生成的 combineLatest Observables 相结合
【发布时间】:2019-10-25 10:22:38
【问题描述】:

我正在尝试组合可观察对象,其中每个可观察对象在 for 循环中获取输入。我的问题是,如果我知道for loop 会提前循环的数组,我会知道如何做到没有for loop -> 我会将所有内容都放在@987654323 中@。

如果我不知道sections 的大小,有人知道我会怎么做吗?

非常感谢!

getArticleSectionsContent(pageId: string): Observable<any> {
 return this.getArticleSections(pageId).pipe(
  switchMap(sections => {
    return combineLatest([
      this.getArticleSectionContent(pageId, sections[0].index),
      this.getArticleSectionContent(pageId, sections[1].index),
      this.getArticleSectionContent(pageId, sections[2].index),
    ]).pipe(
      map(([a, b, c]) => {
        return { a, b, c };
      })
    );
  })
 );
}

【问题讨论】:

  • 有人有想法吗?

标签: loops angular2-observables combinelatest


【解决方案1】:

如果您不知道节的大小,请使用 map 运算符循环遍历您的数组,将其转换为 observables 数组,如下所示:

getArticleSectionsContent(pageId: string): Observable<any> {
 return this.getArticleSections(pageId).pipe(
  switchMap(sections => {
    const articleSectionContentObsArray = sections.map(section => {
        return this.getArticleSectionContent(pageId, section.index);
    });
    return combineLatest(articleSectionContentObsArray);
  })
 );
}

【讨论】:

    猜你喜欢
    • 2018-12-25
    • 2017-03-27
    • 2020-01-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-04
    • 2017-09-07
    • 1970-01-01
    相关资源
    最近更新 更多