簡體   English   中英

嘗試使用 Angular 中的 CombineLatest 將兩個可觀察流與鍵合並

[英]Trying to merge two observable streams with keys using CombineLatest in Angular

我有兩個從 firebase 得到的觀察值。 這兩個 observable 有兩個公共變量:一個是 bankAccountId,另一個是 linkTobankAccountId。 我需要將這些 observable 合並到具有以下結構的 object 中:

{
   {
       bankAccounts:
           {
               bankAccountId1
               ...bankFields
                   }, 
           {    
               bankAccountId2
               ...bankFields 
               }
},
   {    debitCards:
           {
               linkToBankAccountId1
               ...debitCardfields
               }
   }
}

我沒有很多合並可觀察對象的經驗。 我嘗試了很多事情,但到目前為止我還沒有成功。 我在語法上有問題——特別是對我用作鍵的變量進行“內連接”。 這是我的代碼:

this.vm$ = combineLatest([
      this.caregiverBankAccounts$
    .pipe(switchMap(banks =>
          banks.bankAccountId)),
  this.caregiverDebitCards$.pipe(
    map(([debit, linkTobankAccountId]) => {
    linkTobankAccountId === bankAccountId;
}))
]);

我意識到語法是錯誤的。 我收到一個錯誤,它找不到bankAccountId。 我不知道如何使它工作。

在下面的 function 中,我正在映射 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];


        }

       ));

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM