incubator-couchdb-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Chris Anderson" <>
Subject Re: Javascript views don't support for loops?
Date Sun, 06 Apr 2008 03:22:02 GMT

I had a similar problem, and found the solution in declaring the var
in my for loop. Like this:

       if (doc.class == "Entry"){
               for (var n in doc.scores){
                       map(doc.user, doc.scores[n]);

Oh but that was with an Object. Going on to read that you are using an
array, you could try a loop like this:

       if (doc.class == "Entry"){
               for (var i=0, s; s = doc.scores[i]; i++) {
                       map(doc.user, s);

Although that second one is just copied from my client side code, so
it might not be the most efficient way to iterate in Spidermonkey.

hope this helps,


On Sat, Apr 5, 2008 at 8:07 PM, Guby <> wrote:
> Hi
>  I am trying to write a view where I want a for loop to loop through one of
> the parameters in the document and then create several entries for the
> document based on the parameters content.
>  The view would look something like this:
>  function(doc){
>         if (doc.class == "Entry"){
>                 for (n in doc.scores){
>                         map(doc.user, doc.scores[n]);
>                 }
>         }
>  }
>  where doc.scores is an array.
>  When I am inserting a for (x in y) or for (n = 0; n<10, n=n+1) or anything
> of the kind the view just doesn't execute at all!
>  Is this a known limitation?
>  I have to create several entries for each document based on this array!
>  Is there a good workaround you guys could suggest?
>  Best regards
>  Sebastian

Chris Anderson

View raw message