尝试使用 Angular 中的 CombineLatest 将两个可观察流与键合并

我有两个从 firebase 获得的 observable。这两个 observable 有两个公共变量:一个是 bankaccountId,另一个是 linkTobankaccountId。我需要将这些 observable 合并到一个具有以下结构的对象中:

{
   {
       bankaccounts:
           {
               bankaccountId1
               ...bankFields
                   },{    
               bankaccountId2
               ...bankFields 
               }
},{    debitCards:
           {
               linkToBankaccountId1
               ...debitCardfields
               }
   }
}

我在合并 observable 方面没有很多经验。我尝试了很多东西,但到目前为止我还没有成功。我在语法上遇到了问题 - 特别是对我用作键的变量进行“内部连接”。 这是我的代码:

this.vm$ = combineLatest([
      this.caregiverBankaccounts$
    .pipe(switchMap(banks =>
          banks.bankaccountId)),this.caregiverDebitCards$.pipe(
    map(([debit,linkTobankaccountId]) => {
    linkTobankaccountId === bankaccountId;
}))
]);

我意识到语法错误。我收到一个错误,提示它找不到 bankaccountId。我不知道如何让它发挥作用。

mayflow 回答:尝试使用 Angular 中的 CombineLatest 将两个可观察流与键合并

在下面的函数中,我映射了 observables,然后将它们加入到索引中。

this.vm$ = combineLatest([
      this.caregiverBankAccounts$,this.caregiverDebitCards$,])
      .pipe(map(([bank,debit]) => {
        const combinedStream = bank.map((item,i) => Object.assign({},item,debit[i]));
        console.log(combinedStream);
        return [...combinedStream];


        }

       ));
本文链接:https://www.f2er.com/316770.html

大家都在问