I am working on a small app and I have a array with objects and within the objects 2 property's, one called 'label' and one 'value'. What I would like is to add up all values of the property 'value', So that I have one total value.
Vue/JS
data() {
totalRequest: 0,
donutData: [
{ label: 'Openstaande verzoeken', value: 20 },
{ label: 'Geaccepteerde verzoeken', value: 25 },
{ label: 'Afgewezen verzoeken', value: 10 }
],
},
created() {
this.totalRequest = //here goes the function to add up all value's of the property 'value' (total value should be 55)
}
HTML
total value {{ totalRequest }}
So in this example I have 3 objects with a total value of 55 (all 3 property 'value'). How can I achieve this? Thanks in advance.
Answer by dashton, reproduced for vue
created() {
this.totalRequest = this.donutData.reduce((acc, item) => acc + item.value, 0);
}
donutData.Reduce( (acc, item) => { return acc += item.value; }, 0);