2

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 and I cant change the way the array is done.

//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);//

dataType:"json",
cache:false,
success: function(data){
for (i=1; i<?; i++){//Start at 1
//I'm totally lost here.
//Output: "#errOne" "Enter a valid username" ->Loop thru remaining messages
}   
},
1
  • 1
    Careful i is global, use var. Commented Aug 20, 2012 at 6:14

4 Answers 4

2

since your passing data as parameter, you can access the data

try something like this :

var $errors = {};
$errors['success']   = false;
$errors['#errOne']   = "Enter a valid username";
$errors['#errTwo']   = "Enter a valid email";
$errors['#errThree'] = "Enter a valid password";
    data : $errors;
    success: function(data){
    for (var i in data ){
       console.log(i + ':' + data[i]);
    }
Sign up to request clarification or add additional context in comments.

2 Comments

@some i am creating object in the first line itself. and wher do you think i am abusing arrays in javascript?
This is 99% what I needed. Only thing is: any idea how to start this at [1] and not [0]?
2

May be you need this:

success: function(data){
    $.each(data, function(key, value) { 
        alert(key + ': ' + value); 
    });
}

2 Comments

I started off which $.each. Would be really nice if we could tell $.each where to start- at [0] or [1]. Eg: for (i=1;...
In different browsers sorting of Data will be different. In FF it will be as is, but Chrome will sort Data by keys automatically. So in different browsers first element will be different
0
for (var key in data){
   alert(key); // gives keys  success, #errOne, #errTwo, #errThree
   alert(data[key]); // gives values  false, Enter a valid username, Enter a valid email, Enter a valid password
}  

1 Comment

when using for (var key in data) can we tell it to start at [1] and not [0]? Eg: for (i=1;...
0

If your PHP code is like this:

// Use $errors = array(...) if your PHP version is < 5.4
$errors = [
    'success' => false, // Why is 'success' part of the $errors array?
    'error_1' => 'Enter a valid username.',
    'error_2' => 'Enter a valid email.',
    'error_3' => 'Enter a valid password.'
];

print json_encode($errors);

, then in your JavaScript, you can do the following:

$.ajax({
    'url': 'your.url.com/data.json',
    'dataType': 'json',
    'cache': false, // Why are you doing this?
    'success': function (json_data) {
        var i = json_data.length;
        while (i--) {
            alert(json_data[i]); // Or whatever you want to do with the data.
        }
    }
});

I'm pretty sure this is one of the best ways to do this. Someone please correct me if I'm wrong.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.