I've encountered an issue where using Vuejs and Lodash in conjunction with a computed property that calculates the sum of a property in a collection results in unexpected behavior. Instead of summing the values, it seems to concatenate the string [object Object]
.
To illustrate this problem, I have created a jsfiddle with the following code:
new Vue({
el: "#app",
data: {
todos: [
{ text: "Learn JavaScript", time: 5 },
{ text: "Learn Vue", time: 10 },
]
},
computed: {
additup() { return _.sum(this.todos, todo => todo.time ) },
additup2() { return _.sum(this.todos, function (todo) {
return parseInt(todo.time);
})},
additup3() {
var t = 0;
_.each(this.todos, function(todo) { t+=todo.time; });
return t;
}
}
})
The output from the above code snippet is as follows:
Method 1 gives: [object Object][object Object]
Method 2 gives: [object Object][object Object]
Method 3 gives: 15
This issue raises the question of whether there is a solution to resolve the _.sum()
problem or if it requires a deeper understanding of why it behaves in this manner.