[devtalk] looping through an array in JavaScropt

Ross Clutterbuck ross.clutterbuck at gmail.com
Thu Oct 29 21:18:35 GMT 2009


Hi Tris

Can't do a test myself but two things jump out at me:

First, do you need the double loop? You're for looping through the whole 
array and on each iteration you're doing it again with a for...in so 
you're basically going through the whole array 6 times

Also, I think you have your for...in the wrong way around - you want 
each value in the array so should that actually be for(value in getErrors) ?

Do you need both loops?

Ross


Tris wrote:
> Hi all..
> Major Javascript woes today...
> I'm learning.. well, relearning Ajax.. I've a from that IU wanna
> display dynaimic errors..
> Anyhoo, I've prepared an array of my errors in an array (inputId : message)
> I want to loop through that array, and then IF there's a message
> value, then echo it...
> For this example, i'm just echoing some alerts.. but I'm not getting any...
>
> What ROOKIE mistake am I making today? ;-p
> Peace and love...
> Tris..
>
> ==================================
>
> <script type="text/javascript">
>     alert('start');
>
>     var getErrors = new Array();
>     var getErrors = {"lookingWhat":"Who are you looking for?",
>                             "genderWhat":"What gender are you?",
>                             "passwordDifferent":"",
>                             "emailRegistered":"",
>                             "emailInvalid":"Invalid Email Address",
>                             "FieldsEmpty":"Pleace complete all fields"};
>
>     for (i=0;i<getErrors.length;i++) {
>         for(getErrors in value) {
>             var getKey = $('#'+getErrors[i]);
>             var getVal = value[i];
>             if (getVal.val() == '') {
>                 alert('is an error');
>             } else {
>                 alert('no error');
>             }
>         }
>     }
>
> alert('end');
> </script>
> _______________________________________________
> devtalk mailing list
> devtalk at lists.preshweb.co.uk
> http://lists.preshweb.co.uk/mailman/listinfo/devtalk
>
>   



More information about the devtalk mailing list