How does underscorejs reduce work?
It's simple to get
_.reduce([1, 2, 3], function(memo, num){ return memo + num; }, 0); (the result is 6).
But how do the other optional parameters work? In the docs it says:
Memo is the initial state of the reduction, and each successive step of it should be returned by iterator. The iterator is passed four arguments: the memo, then the value and index (or key) of the iteration, and finally a reference to the entire list."
But I don't understand. I tried to use reduce for the following problem, and I couldn't figure it out:
var input = [{"score": 2, "name": "Jon", "venue": "A"}, {"score": 3, "name": "Jeff", "venue":"A"}, {"score": 4, "name": "Jon", "venue":"B"}, {"score": 4, "name": "Jeff", "venue":"B"}];
var output = [{"score": 6, "name":"Jon", "venue": ["A", "B"]}, {"score": 7, "name":"Jeff", "venue": ["A", "B"]}];
How can I get as output using _reduce for input? And it will really helpful how it works inside reduce.
memois simply the initial value at first, then the previous return value for the subsequent iterations. The rest of the arguments are the typical args you'd have for methods like.map()or.forEach()