【发布时间】:2021-06-16 21:57:50
【问题描述】:
我正在使用具有 formArray 的响应式表单,该表单又包含多个 formGroups。
对于每个 formGroup,我得到一个对象数组(取决于他们在年龄字段中选择的内容),例如:
对于 formGroupName[0] 我得到:
[
0: {aseguradora: "name1", plan: "plan1", prima: 6670}
1: {aseguradora: "name2", plan: "plan2", prima: 3272}
]
对于 formGroupName1 我得到:
[
0: {aseguradora: "name1", plan: "plan1", prima: 9302}
1: {aseguradora: "name2", plan: "plan2", prima: 4616}
]
等等……
如何获得一个包含prime 字段总和的新数组?考虑到如果消除了 formGroup,则必须重新计算总和。
也就是说,我想要的结果是:
[
0: {aseguradora: "name1", plan: "plan1", prima: 15972}
1: {aseguradora: "name2", plan: "plan2", prima: 7888}
]
尝试了以下方法,如果你做了总和,但是当一个 formGroup 被删除时,它不会减去那个差:
control.at(+i).get('edad').valueChanges.subscribe( res => {
// Here I get the Array
this.primasFilter = this.primasSalud.filter(fil => fil.edad === res);
this.primasFilterEdad = this.primasFilter[0].primas;
console.log(this.primasFilterEdad);
// Here I do the sum
this.saludPreferencial = (this.primasFilterEdad[0].prima);
this.totalSumSaludP += this.saludPreferencial;
console.log(this.totalSumSaludP);
});
【问题讨论】:
标签: angular typescript angular-reactive-forms