SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Enthusiast
    Join Date
    Dec 2007
    Posts
    36
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Iterating thru an array using javascript for

    How do I iterate thru an array like this using 'for' in jquery. This one has me totally lost. I cant figure how to get those values out from an array like this

    Code:
     //Php
        $errors['success']   = false;
        $errors['#errOne']   = "Enter a valid username";
        $errors['#errTwo']   = "Enter a valid email";
        $errors['#errThree'] = "Enter a valid password";
        echo json_encode($errors);//
    Code:
    dataType:"json",
        cache:false,
        success: function(data){
        for (i=1; i<?; i++){//Start at 1
        //I'm totally lost here.
        //#errOne "Enter a valid username" ->Loop thru remaining messages
        }   
    	},

  2. #2
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,684
    Mentioned
    99 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by Nordy View Post
    How do I iterate thru an array like this using 'for' in jquery.
    You would check that the index is less than data.length as the loop condition, and start from 0 as that's the first item in an array.

    However, that is just with numeric arrays. You have non-numeric keys being used, so you will need to loop through the objects of that array instead.

    Code javascript:
    var key;
    for (key in data) {
        if (data.hasOwnProperty(key)) {
            // use data[key] here
        }
    }
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  3. #3
    SitePoint Enthusiast
    Join Date
    Dec 2007
    Posts
    36
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks, Paul. This is just what I was looking for. Can't believe I couldn't figure this out myself Thanks for helping.


Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •