Calculate and Decrease Array Values

Asked

Viewed 315 times

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?

  • @Ricardopunctual I’m decreasing with splice function within LSE

  • 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 or 0,1,2 depending on index, and you pass this index to Function in the click with the calc(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 the id_servicefor Function calc to remove the correct element.

1 answer

1


Since the intention is to add up the value of price_service of all items containing the property qtd equal to 1, there is no need to create a new object. Just filter the object orders using the method filter and recover all items containing the property qtd equal to 1, then use the method reduce.

I took the liberty of making some modifications, to facilitate, I removed all double quotes from properties that are number and boolean.

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

Create a variable to store the sum value:

total: number = 0;

His method calc was like this:

calc(i) {
  this.orders[i].qtd = (this.orders[i].qtd === 0) ? 1 : 0;
  this.total = this.orders.filter(item => item.qtd === 1).reduce((a, b) => a + b.price_service, 0);
  console.log(this.total);
}

Explanation

When performing this.orders.filter(item => item.qtd === 1) a new array is returned with the elements that passed the test implemented by the given method, which in this case is: item => item.qtd === 1.

In the method reduce the sum of the value of a with the value of the property price_service of the element b.

You can see running on stackblitz

  • I could not implement, because the data comes from the database, and it seems to come inside the object with double quotes = Qtd: "0". I believe that’s why I’m not able to do, I did local without the quotes and it worked, now I do not know how to treat these properties coming from the bank, would help me?

  • worse than price_service tb comes like this, with quotes, and within the function filter did not accept quotes, nor simple...

  • So parseint(b.price_service) was also not, so I tried parseint(b[i].price_service) and it was, but it didn’t work, I believe it is because the function filter does not accept quotes...

  • 1

    See working here stackblitz

  • vlw brother!!!!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.