Loop array and return data for each id in Observable

前端 未结 2 790
旧时难觅i
旧时难觅i 2020-12-25 08:46

Using RxJS v6 is challenging to retrieve data from sub collection for every item in the loop. There is no way to iterate throw array that is retrieved from HTTP call. Merge

2条回答
  •  情深已故
    2020-12-25 09:37

    Although kvetis's answer can work for you here is what I implemented, just posting it because I found it interesting to solve.

      public getCombinedData(): Observable {
        return this.getMultipleRelationData()
          .pipe(
            mergeMap((result: any) => {
              let allIds = result.contact.map(id => this.getSingleData(id._id));
              return forkJoin(...allIds).pipe(
                map((idDataArray) => {
                  result.contact.forEach((eachContact, index) => {
                    eachContact.relationship = idDataArray[index];
                  })
                  return result;
                })
              )
            })
          );
      }
    

    Here is an example solution for your question, I have made dummy Observables. https://stackblitz.com/edit/angular-tonn5q?file=src%2Fapp%2Fapi-calls.service.ts

提交回复
热议问题