2017-08-24 84 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; 
}