I have a computed property function called: Total, this essentially calculates the name + value pairs of an array called prices, it's for a quotation form whereby a running total is added up as a user progresses through a multi-step form, values are then pushed into an array to keep things clean.
This computed property allows me to echo the total on the page which is dynamically updated without any additional code, I can simply add total using handlebars to wherever I want on my page like so: {{ total }}
The problem I'm now facing is that I also want the value of 'total' to be included in a separate array, or at least added to an array and I can't seem to get it right.
The working code which gets the values of my prices array which is by default empty is as follows:
computed: {
total: function(){
var total = [];
Object.entries(this.prices).forEach(([name, value]) => {
total.push(value)
});
return total.reduce(function(total, num){
return total + num
}, 0);
}
}
I want to do add something like this to my computed property:
this.quote.totalPrice = total
I've tried:
computed: {
total: function(){
var total = [];
Object.entries(this.prices).forEach(([name, value]) => {
total.push(value)
});
return total.reduce(function(total, num){
return total + num
this.quote.totalPrice = total
}, 0);
}
}
I'm not getting anything with this?
this.quote.totalPrice = totalis after areturnand unreachable, but you can't do this anyway. Computed properties are standalone and not meant to alter state. Why do you need to this? Either expand your computed property to include the rest ofthis.quote, or just fetchtotalwhen you need to construct yourquoteobject