通过多维数组JS上的while循环获取未捕获的TypeError

时间:2015-03-30 13:43:18

标签: javascript jquery

我正在创建一个多维数组,并将数组的特定部分添加到变量中。数学出现正确并且每次都有效,但每次我都会得到一个未捕获的类型错误。具体来说就是这样说的。

 Uncaught TypeError: Cannot read property '2' of undefined

这是正在发生的while循环。

while (i <= theAgents[agentNumber].length) {
            var comm = theAgents[agentNumber][i][2];
            commTotals += comm;
            console.log(commTotals);
            $(this).parent().find("#productionTotals").text(commTotals);
            i++
        };

以下是代码中的部分内容

 $("body").on("click", "#agentButton", function(event) {

        // v1-1 creating an array to put all of the deal info in to so that I can push it to the theAgents array
        var theNewDeal = [];
        var totalDeals = 0;
        var i=0;
        var commTotals = 0;

        var newDealAddress = prompt('Enter the property address'); 
        var newDealContractDate = prompt('Enter the contract date');
        var newDealProduction = prompt('Enter the purchase price');
        var newDealCommission = prompt('Enter the commission');
            newDealProduction = parseInt(newDealProduction);
            newDealCommission = parseFloat(newDealCommission);
        var CommissionRate = newDealCommission/100;
        var newDealGCI = newDealProduction * CommissionRate;

       $(this).parent().find("#address").val(newDealAddress);
       $(this).parent().find("#production").val(newDealProduction);
       $(this).parent().find("#contractDate").val(newDealContractDate);
       $(this).parent().find("#commission").val(newDealCommission);
       $(this).parent().find("table.agent").append("<tr><td>"+newDealAddress+"</td><td>"+newDealContractDate+"</td><td class='production'"+
         "contentEditable='true'>$"+newDealProduction+"</td><td class='commission'>"+newDealCommission+"%</td><td>$"+newDealGCI+"</td></tr>");

      //This is the push to theNewDeal array
      theNewDeal.push(newDealAddress, newDealContractDate, newDealProduction, newDealCommission, newDealGCI);
        console.log(theNewDeal);

        agentCommission.push(newDealProduction);


       //Writing out the data-agent value to figure out how to get it so I can link the correct button to the right table.

       var agentNumber = $(this).attr("data-agent");


       //pushing data in to the proper theAgents array location.
        theAgents[agentNumber].push(theNewDeal);
       console.log("He has this many deals", theAgents[agentNumber].length+" And his agent number is "+agentNumber);
       console.log(theAgents); 

        //This is where I make the text of the total production table equal to the while loop above.    
        $(this).parent().find("#productionTotals").text(agentCommission);

       //while loop to add up total commissions of each agent every time a new deal is put in. Added in 1-2 
        while (i <= theAgents[agentNumber].length) {
            var comm = theAgents[agentNumber][i][2];
            commTotals += comm;
            console.log(commTotals);
            $(this).parent().find("#productionTotals").text(commTotals);
            i++
        };

代码执行得很好并且每次都有效,但由于某些原因,我在Chrome中遇到了这个错误。这导致代码关闭,并且在while循环之后不能用于任何操作。我还有一些我必须为阵列的不同部分创建的其他部分,所以任何关于它弹出的原因都很棒!

Here is a JSFiddle整个项目,所以你可以在没有我发布的情况下看到整个项目。谢谢!

4 个答案:

答案 0 :(得分:3)

关闭一个错误,索引从零开始。

while (i <= theAgents[agentNumber].length) {
        ^^^^

需要

while (i < theAgents[agentNumber].length) {
        ^^^^

答案 1 :(得分:1)

while (i < theAgents[agentNumber].length) {
        var comm = theAgents[agentNumber][i][2];
        commTotals += comm;
        console.log(commTotals);
        $(this).parent().find("#productionTotals").text(commTotals);
        i++
    };

我应该小于长度,因为它从0开始。

答案 2 :(得分:0)

检查是否有theAgents[agentNumber],而不是检查是否theAgents[agentNumber][i]然后获取theAgents[agentNumber][i][2]的值或将comm设置为0

while (i <= theAgents[agentNumber].length) {
   var comm = (theAgents[agentNumber] && theAgents[agentNumber][i] && theAgents[agentNumber][i][2]) || 0;
   commTotals += comm;
   console.log(commTotals);
   $(this).parent().find("#productionTotals").text(commTotals);
   i++
};

答案 3 :(得分:0)

while (i <= theAgents[agentNumber].length) // should be < and not <=

,因为:

var arr = [1,2,3]
arr.length === 3     // true
arr[3] === undefined // true