You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@couchdb.apache.org by Oliver Boermans <bo...@gmail.com> on 2009/05/16 04:23:32 UTC

A reduce with multiple totals

Hi CouchCrew.

Hoping you guys can help me with my first reduce composition problem.

My app is intended to log phone calls. Now I want to output some
tallies. For each logged call I am emitting two items: one each for
the call answered and the call’s intended recipient. I want a tally of
calls answered and calls for with the phone answerer/recipient as the
key.

Please take a look at the code here:
http://www.friendpaste.com/64pGMIf7OoAOkSvRew8AOB

The results are null instead of my intended total. What am I doing
wrong in my reduce function?

My approach feels good up until reduce. Please correct me if there is
a better angle to this problem.

I’m spoilt by Firebug as a tool for troubleshooting this kind of code
in a browser. I’d be very interested to hear what approach you might
use to nut out what is going wrong when composing something like this
in Futon.

Many thanks
Ollie

Re: A reduce with multiple totals

Posted by Oliver Boermans <bo...@gmail.com>.
2009/5/16 Patrick Antivackis <pa...@gmail.com>:
> You should do like this :
>
>
> function(keys, values) {
>  var totalcallsfor = 0;
>  var totalanswered = 0;
> values.forEach(function(value) {
>    totalcallsfor = totalcallsfor + value.callfor;
>    totalanswered = totalanswered + value.answered;
> });
>  return {'callfor': totalcallsfor, 'answered':totalanswered};
> }

That works, Thank you Patrick.

Re: A reduce with multiple totals

Posted by Patrick Antivackis <pa...@gmail.com>.
Hello Oliver,
You are mistaking on the way to iterate over the values object.
You should do like this :


function(keys, values) {
  var totalcallsfor = 0;
  var totalanswered = 0;
values.forEach(function(value) {
    totalcallsfor = totalcallsfor + value.callfor;
    totalanswered = totalanswered + value.answered;
});
  return {'callfor': totalcallsfor, 'answered':totalanswered};
}

Regards


2009/5/16 Oliver Boermans <bo...@gmail.com>

> Hi CouchCrew.
>
> Hoping you guys can help me with my first reduce composition problem.
>
> My app is intended to log phone calls. Now I want to output some
> tallies. For each logged call I am emitting two items: one each for
> the call answered and the call’s intended recipient. I want a tally of
> calls answered and calls for with the phone answerer/recipient as the
> key.
>
> Please take a look at the code here:
> http://www.friendpaste.com/64pGMIf7OoAOkSvRew8AOB
>
> The results are null instead of my intended total. What am I doing
> wrong in my reduce function?
>
> My approach feels good up until reduce. Please correct me if there is
> a better angle to this problem.
>
> I’m spoilt by Firebug as a tool for troubleshooting this kind of code
> in a browser. I’d be very interested to hear what approach you might
> use to nut out what is going wrong when composing something like this
> in Futon.
>
> Many thanks
> Ollie
>