How does underscorejs reduce ?
Just get _.reduce([1, 2, 3], function(memo, num){ return memo + num; }, 0); (result 6 ).
But how do other optional parameters work? The docs say:
The memo is the initial state of contraction, and each subsequent step must be returned by the iterator. Four arguments are passed to the iterator: a memo, then the value and index (or key) of the iteration, and finally a link to the entire list.
But I do not understand. I tried using reduce for the following problem, and I could not understand:
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 output using _reduce for input? And it really will help how it works inside to reduce.