Saturday, 19 November 2016

jquery - 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 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'm totally lost here.
//Output: "#errOne" "Enter a valid username" ->Loop thru remaining messages
}
},

Answer



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]);
}

No comments:

Post a Comment

c++ - Does curly brackets matter for empty constructor?

Those brackets declare an empty, inline constructor. In that case, with them, the constructor does exist, it merely does nothing more than t...