You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@couchdb.apache.org by Guby <gu...@gmail.com> on 2008/04/06 05:07:18 UTC

Javascript views don't support for loops?

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

Re: Javascript views don't support for loops?

Posted by Guby <gu...@gmail.com>.
Sweet!
That first option worked perfectly!
Thank you very much Chris!

Best regards
Sebastian



On Apr 6, 2008, at 12:22 AM, Chris Anderson wrote:

> Sebastian,
>
> I had a similar problem, and found the solution in declaring the var
> in my for loop. Like this:
>
> function(doc){
>       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:
>
> function(doc){
>       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,
>
> Chris
>
>
> On Sat, Apr 5, 2008 at 8:07 PM, Guby <gu...@gmail.com> 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
> http://jchris.mfdz.com


Re: Javascript views don't support for loops?

Posted by Chris Anderson <jc...@mfdz.com>.
Sebastian,

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

function(doc){
       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:

function(doc){
       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,

Chris


On Sat, Apr 5, 2008 at 8:07 PM, Guby <gu...@gmail.com> 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
http://jchris.mfdz.com