簡體   English   中英

Angular2過濾器對象數組

[英]Angular2 filter array of objects

我將首先添加代碼,然后得到結果,最后我想獲得什么,如果可能的話。

我得到的結果是Array [object,object,...]其中object是Array

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]
        });
    ....
    }
}

過濾器結構

過濾器結構

我正處於angular2和反應式編程的早期學習階段,我想過濾一下,這樣映射結果將僅是具有至少一項的數組。

謝謝

代替.filter使用.map

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

不幸的是,這不適用於forkJoin 它的作用是將多個Observable合並為單個 Observable,因此,如果其中的任何一個被過濾掉,整個連接的Observable get都會被中斷/過濾。

如@Martin所述,您必須在map分支中進行過濾。

暫無
暫無

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

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