Reduce with multiple totals Revision 663966663965 (Sat May 16 2009 at 02:10) - Diff Link to this snippet: https://friendpaste.com/64pGMIf7OoAOkSvRew8AOB Embed: manni perldoc borland colorful default murphy trac fruity autumn bw emacs pastie friendly Show line numbers Wrap lines 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556// I am putting together an app to log phone calls// the significant (portion) my docs look like[ {_id: …, _rev: …, answered: "maurice", callfor: "roy", type: "call"}, {_id: …, _rev: …, answered: "maurice", callfor: "jen", type: "call"}, {_id: …, _rev: …, answered: "roy", callfor: "jen", type: "call"}]// for each call I am emitting two items: one each for the call answered and the call’s intended recipient.// mapfunction(doc) { if(doc.type == 'call') { emit(doc.callfor, {'callfor':1,'answered':0}); emit(doc.answered, {'callfor':0,'answered':1}); }}// Which outputs something like[ "maurice": {callfor:0, answered: 1}, "maurice": {callfor:0, answered: 1}, "roy": {callfor:1, answered: 0}, "roy": {callfor:0, answered: 1}, "jen": {callfor:1, answered: 0}, "jen": {callfor:1, answered: 0}]// I want a tally of calls answered and calls for with the recipient as the key// reducefunction(keys, values) { var totalcallsfor = 0; var totalanswered = 0; for (var call in values) { totalcallsfor = totalcallsfor + call.callfor; totalanswered = totalanswered + call.answered; } return {'callfor': totalcallsfor, 'answered':totalanswered};}// Outputs something like[ "maurice": {callfor: null, answered: null}, "roy": {callfor: null, answered: null}, "jen": {callfor: null, answered: null}]// Where I expected (hoped)[ "maurice": {callfor: 0, answered: 2}, "roy": {callfor: 1, answered: 1}, "jen": {callfor: 2, answered: 0}]// What am I doing wrong in my reduce function?