Revision 663966663965 () - Diff

Link to this snippet: https://friendpaste.com/64pGMIf7OoAOkSvRew8AOB
Embed:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
// 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?