[thelist] Fwd: Javascript array loop issues?

Jay Turley jayturley at gmail.com
Thu Oct 29 14:40:32 CDT 2009


Ever have one of those days where no matter what you write, it's wrong?

Length() should be just length.

<tip type="reputation">
Don't answer problems unless you take the time to make sure you  
understand both the problem AND your answer.
</tip>

Le sigh



Begin forwarded message:

> From: Jay Turley <jayturley at gmail.com>
> Date: October 29, 2009 12:32:15 PM PDT
> To: "thelist at lists.evolt.org" <thelist at lists.evolt.org>
> Subject: Re: [thelist] Javascript array loop issues?
>

> Yes, as I shamefacedly pointed out in my second email (which I then  
> screwed up as well), replying from your phone is a sure way to lower  
> your street cred.
>
> If I were doing this, using the idea that Tris was demonstrating in  
> her initial email, I would try something along the following lines:
>
> <input id="name" type="text" />
> <input id="email" type="text" />
>
> var getErrors = [
>  {
>   errorKey: 'name',
>   errorMsg: 'Please enter your name'
>  },
>  {
>   errorKey: 'email',
>   errorMsg: 'Please enter your email'
>  }
> ];
>
> for ( var i=0, j=getErrors.length(); i<j; i++ ) {
>  var $formElement = $('#'+getErrors[i].errorKey);
>  if ( $formElement.val() == '' ) {
>   alert(getErrors[i].errorMsg);
>  }
> }
>
> or something...
>
> at least that looks reasonably correct in my text editor... ON MY  
> COMPUTER :(
>
> On Thu, Oct 29, 2009 at 11:07 AM, Christian Heilmann <codepo8 at gmail.com 
> > wrote:
> Jay Turley wrote:
> > I think your problem may be that you are creating an object  
> literal by
> > using the braces {} instead of the brackets [] to create your array
> > elements. Try it like this:
> >
> > var getErrors = [ errorName: errorMsg, ...];
> >
> > Jay
> >
> >
> >
> No, a "associative array" is a misnomer as in JS it is an object. You
> cannot loop over the properties of an object using length, what you  
> need
> to do is using a for...in loop.
>
> http://stackoverflow.com/questions/921789/how-to-loop-through-javascript-object-literal-with-objects-as-members
>
>
> --
>
> * * Please support the community that supports you.  * *
> http://evolt.org/help_support_evolt/
>
> For unsubscribe and other options, including the Tip Harvester
> and archives of thelist go to: http://lists.evolt.org
> Workers of the Web, evolt !
>



More information about the thelist mailing list