1
I am trying to decrement and calculate values of an array unsuccessfully. The decrementation works to a certain extent, so it decreases different values than what is clicked on the checkbox and the calculation appears on the console as Nan.
orders = [
{id_service: "1", id_empresa: "9", nome_service: "Servico 01", qtd: "0", checkup: "false", price_service : "250"},
{id_service: "2", id_empresa: "9", nome_service: "Servico 02", qtd: "0", checkup: "false" price_service : "300"},
{id_service: "3", id_empresa: "9", nome_service: "Servico 03", qtd: "0", checkup: "false" price_service : "400"}
]
calc(i) {
let obj = JSON.parse(this.orders[i].price_service);
if (this.orders[i].qtd == 0) {
this.result.push(obj);
this.orders[i].qtd = 1;
}
else {
this.result.splice(i, 1);
this.orders[i].qtd = 0;
}
this.result.reduce(function (a, b) {
return a + b['obj'];
}, 0);
}
<div *ngFor="let item of orders; let i = index; ">
<ion-item>
<ion-label class="title">{{item.nome_service}}
<span>{{item.price_service}}</span>
</ion-label>
<ion-checkbox (click)="calc(i)" checked="{{item.checkup}}" color="green"></ion-checkbox>
</ion-item>
<p>{{result}}</p>
</div>
I did not understand where you are decreasing the value in the code, I could understand that you are only setting "0" or "1", can explain better?
– Ricardo Pontual
@Ricardopunctual I’m decreasing with splice function within LSE
– Henrique Mendes Silveira Rodri
I saw this line, so you are removing items from the array. This will not work because, when you run Binding from
angular
, the indices shall be sequential, i.e.:1,2,3
or0,1,2
depending onindex
, and you pass this index to Function in the click with thecalc(i)
. If you click the first item for example, it will remove element 1, so element 2 becomes the first. What will happen when you click on element 2? The second element will be removed, which is now item 3! Try to pass theid_service
for Functioncalc
to remove the correct element.– Ricardo Pontual