I even learned that using for (... in ...) is not safe for use with arrays. I 
tend to use Array.forEach() [0] like this:

----
function(doc) {
  if (doc.users) {
    doc.users.forEach(function(user) {
      emit(user, doc);
    });
  }
}
----

I'm not very proficient in JavaScript, so maybe there is no real in advantage 
in the realm of CouchDB.

Nils Breunese.

[0] 
https://developer.mozilla.org/En/Core_JavaScript_1.5_Reference:Objects:Array:forEach

________________________________________
Van: Jan Lehnardt [...@apache.org]
Verzonden: woensdag 2 september 2009 21:54
Aan: user@couchdb.apache.org
Onderwerp: Re: View to find someone in a list in a document

Style police!

On 2 Sep 2009, at 19:00, Simon Metson wrote:

> Hi,
>       Do you mean you want to emit each user as a key? Something like:
>
> function(doc) { for (v in doc.users) { emit(doc.users[v], doc); }

is better written as

function(doc) { for (var v in doc.users) { emit(doc.users[v], doc); }

if you leave out the `var` you create a global variable (as opposed to
a local variable in JS and that can have funky effects.

better yet:

function(doc) { if(doc.users) { for (var v in doc.users) { emit
(doc.users[v], doc); }}

protects you against errors for documents that doesn't have a `users`
property.

otherwise, excellent advice! :)

Cheers
Jan

De informatie vervat in deze  e-mail en meegezonden bijlagen is uitsluitend 
bedoeld voor gebruik door de geadresseerde en kan vertrouwelijke informatie 
bevatten. Openbaarmaking, vermenigvuldiging, verspreiding en/of verstrekking 
van deze informatie aan derden is voorbehouden aan geadresseerde. De VPRO staat 
niet in voor de juiste en volledige overbrenging van de inhoud van een 
verzonden e-mail, noch voor tijdige ontvangst daarvan.

Reply via email to