[Vuejs]-Why does lodash's _.sum() function fail Vuejs objects

2👍

Use sumBy instead of sum:

This method is like _.sum except that it accepts iteratee which is
invoked for each element in array to generate the value to be summed.
The iteratee is invoked with one argument: (value).

👤CD..

2👍

Try this instead:

additup() { return _.sum(this.todos.map(todo => todo.time)) },
👤kots

1👍

Are you set on _.sum and then mapping? Because you can simply do:

additup() { return _.sumBy(this.todos, 'time') }

Or with ES6 you could get even shorter by:

additup = () => _.sumBy(this.todos, 'time')

This is actually _.sumBy main use-case.

👤Akrion

Leave a comment