I'm currently developing a small app and have an array of objects with two properties each: 'label' and 'value'. I want to calculate the total value by adding up all the values in the 'value' property.
Vue/JS
data() {
totalRequest: 0,
donutData: [
{ label: 'Pending requests', value: 20 },
{ label: 'Accepted requests', value: 25 },
{ label: 'Rejected requests', value: 10 }
],
},
created() {
this.totalRequest = //Functionality needed to sum up all values in the 'value' property (total should be 55)
}
HTML
total value {{ totalRequest }}
In this example, there are 3 objects with a total value of 55 (the sum of all 3 'value' properties). How can I achieve this? Thank you in advance.
Answer by dashton, adapted for vue
created() {
this.totalRequest = this.donutData.reduce((acc, item) => acc + item.value, 0);
}