'Calculate column quantity total in angular
I have an array of objects that contain a quantity field. I'm looking to total one field from the array on a review screen on a table.
I have an array of administrations populated with object fields and the one I'm looking to calculate is the 'quantity field'.
<tr class="primary" *ngFor="let medication of sachets">
<td>{{medication.administration.quantity}}</td>
<td>
{{medication.medicationName}}
<p class="instructions">{{medication.additionalInstructions}}</p>
</td>
</tr>
Within the table, I am calculating the length of entries using angulars .length
I'm looking to calculate a total for the array of quantities.
Solution 1:[1]
Found a solution to filter and add the values. I used the .map function to create an array of quantity values, and using .reduce, add them together to give the desired total.
totalFunction() {
const initialValue = 0;
this.totalQuantity = this.administered.map
(item => item.administration.quantity).reduce(
(previousValue, currentValue) => previousValue + currentValue,
initialValue);
console.log(this.totalQuantity);
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | DFZ |