I have a simple collection:
[{a: 1}, {a: 2}, {a: 3}]
How do I use Lodash's reduce to get the sum of all "a" attributes?
This seems like a trivial / canonical use, but I can't get the syntax right and surprisingly can't find any docs beyond Lodash's example.
Using Lodash's docs example, it should be:
const total = _.reduce([{ a: 1}, {a: 2}, {a: 3}], (sum, elem) => elem.a);
However this returns the value "3" instead of "6'.
Note: I'm specifically asking about the usage of reduce
. I'm aware of other methods like the one in this question.