I'm currently experimenting with a basic to-do list using vue.js
. My goal is to calculate the total sum of all price
values within an array. I created a small function under computed
, but it seems like something isn't working correctly. Here's the snippet of my js
code:
var app= new Vue({
el: "#app",
data: {
message: "Shopping List",
seen: true,
todos: [
{msg: 'parsley', price: 10},
{msg: 'tomatoes', price: 20},
{msg: 'zucchinis', price: 5}
],
},
methods: {
addToDo: function() {
if(this.name && this.price) {
this.todos.push({msg: this.name, price: this.price});
}
this.name = "";
this.price = "";
},
RemoveThis: function(index) {
this.todos.splice(index,1);
}
},
computed: {
totalSum: function() {
var total = 0;
this.todos.forEach(function(item) {
total += item.price;
});
return total;
}
}
});
There seems to be an issue with the forEach
loop causing the function to break. Any insights on what might be wrong?