2016-11-07 72 views
0

我將開始添加代碼,得到的結果以及最終我想要獲得的結果,以及是否有可能。Angular2過濾器對象陣列

其中我得到的結果是數組[對象,對象,...],其中對象是陣列

export class SomeService { 
      .... 
      .... 
    public someFunction(): MyObject[]{ 
     Observable 
      .forkJoin(this.userItemsA(userId), this.userItemsB(userId), etc) 
      .filter(each => { 
         for (let array of each) { 
          let x: any = <any> array; 
           return x.length > 0; 
          } 
         }) 
      .map(result => { 
        return result; 
       }) 
      .subscribe(result => { 
        /// what i would like to do for example assuming only 1st array has items 
        /// do something here with result[0] 
        /// return MyObject[] from result[0] 
     }); 
    .... 
    } 
} 

濾波器結構

filter structure

I」 m在angular2和反應式編程的早期學習階段,我想要過濾以便映射結果將只有至少有一個項目的數組。

謝謝

+1

你在'.filter(each => ...'中的數據結構如何? – martin

+0

我已經上傳了過濾器的結構,thx – Remus

回答

1

代替.filter使用.map

.map(each => { 
    return each.filter(array => array.length > 0) 
} 
1

不幸的是,這並不與forkJoin工作。它的作用是將多個Observable連接成單個 Observable,因此如果它們中的任何一個被過濾掉,則整個連接的Observable被中斷/過濾。

正如@Martin所述,您必須在map分支中進行篩選。