// 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. // map function(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 // reduce function(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?