Help for sum logic


#1

Hello there.
im develop apps for laundry system.
and i stuck in here

you see i have 2 service. that service data is from web service.
how can i sum it ???
please help


#2

Can you share your code for a more specifically help?


#3

now is just parse dara from web service, im not yet write code for sum

  <ion-row *ngFor="let item of service">
    <div class="row" style="width: 100%;" *ngIf="item.service_type === 'basic' ">
      <div style="text-align: left;margin-left: 10%;width:40%;">
        <h2>{{ item.service_name }}</h2>
        Rp. {{ item.service_price }} / Kg
      </div>
      <div style="width:50%;">
        <ion-item style="margin-left:20%;margin-top: 25px;width: 70px;">
          <ion-input type="number" value="0"></ion-input>
        </ion-item>
      </div>
    </div>
    <hr>
  </ion-row>

#4

You have a lot of ways to do this. I recommend after load the variable ‘service’ with the objects, you call one function to do the sum, like:

// home.ts
  public sumOfServices : number = null;
  public sumServicePrice(){
    this.sumOfServices = 0;
    for(let i = 0; i<this.service.length; i++){
        this.sumOfServices = this.sumOfServices + this.service[i].sumServicePrice;
    }
  }
//call using
this.sumServicePrice();


#5

@HugoPetla.

// home.ts
public sumOfServices : number = null;
public sumServicePrice(){
this.sumOfServices = 0;
for(let i = 0; i<this.service.length; i++){
this.sumOfServices = this.sumOfServices + this.service[i].sumServicePrice;
}
}
//call using
this.sumServicePrice();

sumOfServices is variable from what ??