0

我正在尝试组合 observables,其中每个 observables 在 for 循环中获取输入。我的问题是,如果我知道数组会提前循环,我会知道如何在没有.for loopfor loopcombineLatest

如果我不知道尺寸,有谁知道我会怎么做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 };
      })
    );
  })
 );
}
4

1 回答 1

0

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

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);
  })
 );
}
于 2019-11-21T15:55:34.830 回答