Iterating thru an array using javascript for

This question already has an answer here:

  • How to loop through a plain JavaScript object with the objects as members? 17 answers

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

    可能你需要这个:

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

    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
    }  
    
    链接地址: http://www.djcxy.com/p/52004.html

    上一篇: 如何在JavaScript中打印对象的值?

    下一篇: 使用javascript for循环访问数组