1

Below is the array that I have. I am building an ionic/angular application and want to display on the screen Total which would be a sum of the Quanties in the array of objects below.

This array can have objects added to it as well as the quantities can increase/decrease... but I'm wanting to have this total reflect those changes as they happen. What would be the best way to approach this?

enter image description here

Joe Starnes
  • 471
  • 1
  • 6
  • 20

3 Answers3

1

This one line should do it

let sum = LotsForCheckIn.map(lot => lot.Quantity).reduce((prev, curr) => prev + curr, 0);

UPDATE:

I'm gonna assume here that you have a LotsForCheckIn as a class property in a component somewhere.

component html:

<div>
Total {{sum()}}
</div>

component ts:

@Component({
  selector: "app-root",
  templateUrl: "./app.component.html",
  styleUrls: ["./app.component.scss"]
})
export class AppComponent {
  LotsForCheckIn = [];

  sum(){
    this.LotsForCheckIn
      .map(lot => lot.Quantity)
      .reduce((prev, curr) => prev + curr, 0);
  }
}

Now any change to the list would be reflected in the UI without any additional code.

Sayegh
  • 1,381
  • 9
  • 17
  • that looks nice.... but I'm wanting to somehow watch this array and trigger this above as things are added/removed and quantities are changed... any suggestions? – Joe Starnes Apr 17 '19 at 20:04
  • Yeah, angular has 2 way data binding. So it's pretty much done for you. You wouldn't need any additional code. It would help if you shared more code and specified what you have in mind exactly. – Sayegh Apr 17 '19 at 20:06
  • I have an array as shown above and this array can be added/deleted from via controls... this array is then displayed on a front end application and the quantities show as text box. Above the control I just want simple html that displays total: x which would be where sum goes. I hope that helps – Joe Starnes Apr 17 '19 at 20:08
0

You can do this:

let sum = 0;
LotsForCheckIn.foreach( (found:any) => {
  if (found.Quantity != null){
    sum += found.Quantity;
  }
});
Samuel Philipp
  • 10,631
  • 12
  • 36
  • 56
claudy399
  • 40
  • 5
0

Write below function in your component class.

//  i am assuming below are properties of the component class
totalQuantity:number;
LotsForCheckIn:any[];

calculateTotalQuantity(){
this.totalQuantity = 0;
   this.LotsForCheckIn.forEach((lot:any)=>{
      this.totalQuantity += lot.Quantity;
  });
}

add similar to the following to the template of component class for data binding totalQuantity.

<p>Total Quantity - {{totalQuantity}},</p>

when ever the LotsForCheckIn changes call calculateTotalQuantity(). I have assigned any to the lot and LotsForCheckIn. It is better to define the interface and give that as data type.

Gireesh
  • 468
  • 7
  • 13
  • Awesome... I think thats the part I'm stuck on.... Whats the best way to constantly monitor an array for changes.... changes on properties of an object AND objects being added/removed. – Joe Starnes Apr 17 '19 at 16:02
  • @JoeStarnes check this out https://stackoverflow.com/a/42962723/8164339 – Gireesh Apr 17 '19 at 16:14
  • I like that and was able to listen for changes to the high level array changes..... (add/delete).... but not sure this will give me what I want to check each object in that array and see if a property on any of them has changed. Unless I am misunderstanding. – Joe Starnes Apr 17 '19 at 19:53