Javascript不能使用数组内容作为数组索引?

时间:2015-12-28 15:33:25

标签: javascript arrays

当我想要运行js代码时,它总是说“未捕获的TypeError:无法读取未定义的属性'_id'”

deployedID.push(data[j]._id);

经过一番思考后,我认为问题在于

var j = deploy[i];

有什么办法可以解决吗?提前谢谢。

以下是完整的代码:

    var deploy = [];
    var deployedID = [];
    var deployedename = [];
    var deployedoname = [];
    var deployedrank = [];

function popularizeTable(){
    $.get("/SSS/getlist", function(data){
        deploy = randomunique(data.length ,100);
        for (var i = 0; i < 100; i++){
            var j = deploy[i];
            deployedID.push(data[j]._id);
            deployedename.push(data[j].ename);
            deployedoname.push(data[j].oname);
            deployedrank.push(data[j].rank);
        }
        var str = " ";
        var k = 0;
        for (var i = 0; i < 5; i++){
            str += "<tr>";
            for(var j = 0; j < 20; j++){            
                str += "<td><section id=\"" + deployedID[k] + "\" class=\"container\"><div class=\"card\">"
                str += "<figure class=\"front\"><img src=\"images/back.jpg\"></figure>";
                str += "<figure class=\"back\"><img src=\"images/Clear/" + deployedename[k] + "/001.jpg\"></figure></div></td></section>";
                k++;
            } 
            str += "</tr>";     
        }
        $("#table").append(str);
    });
}

$( document ).ready(function() {
    popularizeTable();
    $(document).on("mouseover", ".card", function(){
        $(this).toggleClass("flipped")});
});

function randomunique(max, num){
    var uniquelist = [];
    for (var j = uniquelist.length; j < num; j++){
        var i = parseInt(Math.random() * (max - 1) + 1);
        if (uniquelist.indexOf(i) == -1){
            uniquelist.push(i);
        }
    }
    return uniquelist;
}

0 个答案:

没有答案