2017-08-24 83 views
0

我正在使用解析器從我的數據庫中獲取與提供的ID相匹配的所有模型數據。 所以,我說給我所有地方detailId = 140,它會返回陣列中的全部結果的模型數據,其中的項目有detailId從解析器數據中獲取特定索引的信息

0: {detailId: 140, area: 4, depth: 1, complete: false} 1: {detailId: 140, area: 5, depth: 4, complete: false} 2: {detailId: 140, area: 6, depth: 8, complete: false} 3: {detailId: 140, area: 7, depth: 10, complete: false} 4: {detailId: 140, area: 8, depth: 3, complete: false} 5: {detailId: 140, area: 9, depth: 3, complete: false}

我想要得到的深度值對於每個特定區域,所以我可以動態地通過formBuilder像這樣分配的值(爲這些目的,面積:4 = index4等)

(我想以某種方式索引/面積值傳遞給函數它在那裏計算?)

this.detailForm = new FormBuilder().group({ 
     index4: [this.getDepth(4)], 
     index5: [this.getDepth(5)], 
     index6: [this.getDepth(6)] 
    }); 

以及沿此線的東西的功能:

getDepth(index: number) { 
    // return depth value where area == index 
} 

我怎樣才能使這項工作?

+0

index4:[this.resultArray [0] .depth] – Vega

+0

@Vega編輯:沒關係,有實例中的索引將會改變,因爲如果任何給定區域的值爲空值,它將不會被加載,這樣就會混淆下面任何條目的索引。 – CodyCS

回答

0

我想通弄明白了:

index4: [this.getDetailAreaDepthByIndex(4)] 

然後

getDetailAreaDepthByIndex(index: number): number { 
    if (!this.detailArea) 
     return null; 

    let filteredItems = this.detailArea.filter(i => i.area === index) || []; 

    return filteredItems.length > 0 ? filteredItems[0].depth : null; 
}