繁体   English   中英

数组迭代的“未捕获的类型错误:无法读取未定义的属性‘长度’”

[英]"Uncaught TypeError: Cannot read property 'length' of undefined" for an array iteration

我有一个简单的三层数组,一次迭代一位。 出于某种原因,在对所有内容进行迭代后,它会出现“未捕获的类型错误:无法读取未定义的属性‘长度’”错误。

违规代码是这样的:

for (k = 0; k < battleplans[i][j].length; k++) {

就目前而言,它按预期处理所有信息,然后抛出错误并停止。

但是如果我用长度属性替换部分,那么更宽的代码是

for (k = 0; k < 1; k++) {
    document.getElementById("BP options").innerHTML += battleplans[i][j][k] + "<br>";
}

它切换到“未捕获的类型错误:无法读取未定义的属性 '0'”,但它再次执行了它在抛出错误之前应该执行的所有操作。

如果我从外部 for 循环中进行迭代以确保它不会尝试迭代不存在的东西,它会丢失位但仍然会出现错误。

我设法找到了一些类似的问题,这些问题说要检查目标是否已定义,但是代码并没有告诉它检查尚未检查并发现存在的任何内容(我已经通过输出验证了这一点)控制台的长度属性值)。 它似乎只是完成所有操作触发错误。

除此之外,我不太了解其他人的代码,无法看到与我的任何相似之处。

任何建议将不胜感激。

编辑:根据要求提供更多信息。

这是作战计划常数:

const battleplans = [
    [ // [0]
        "FIRST BLOOD",
        [ // [0][1]
            "SET-UP",
            "The players roll off, and the winner decides which territory each side will use. The territories are shown on the map below.",
            "The players then alternate setting up units one at a time, starting with the player that won the rolloff to determine territories. Units must be set up wholly within their own territory, more than 12\" from enemy territory.",
            "Continue to set up units until both players have set up their armies. If one player finishes first, the opposing player sets up the rest of the units in their army, one after another."
        ],
        [ // [0][2]
            "FIRST BLOOD",
            "The player in command of the army that first slays an enemy model receives 1 extra command point."
        ],
        [ // [0][3]
            "GLORIOUS VICTORY",
            "The battle continues until one player has no units left on the battlefield, or at the end of the fifth battle round should this occur sooner.",
            "When the battle ends, each player calculates a victory score by adding up the Wounds characteristics of all the models from the opposing army that were slain during the battle. If one player beats their opponent's score by 50% or more, they win a <b>major victory</b>. Otherwise the player with the higher score wins a <b>minor victory</b>."
        ]
    ]
];

这是 for 循环的完整嵌套:

for (i = 0; i < battleplans.length; i++) {
    for (j = 0; i < battleplans[i].length; j++) {
        if (j == 0) {
            var name = battleplans[i][j];
            document.getElementById("BP options").innerHTML += "<input type=\"radio\" id=\"" + name + "\" name=\"battleplan\" value=\"" + name + "\"></input>";
            document.getElementById("BP options").innerHTML += "<label for=\"" + name + "\">" + name + "</label>";
        } else {
            document.getElementById("BP options").innerHTML += "<p>";
            for (k = 0; k < battleplans[i][j].length; k++) {
                document.getElementById("BP options").innerHTML += battleplans[i][j][k] + "<br>";
            }
            document.getElementById("BP options").innerHTML += "</p>";
        }
    }
    document.getElementById("BP options").innerHTML += "<hr>";
}

您在第二个循环中的 for 循环中有错误更改 i < Battleplans[i].length; 到 j < 战斗计划 [i].length;

for (let i = 0; i < battleplans.length; i++) {
    for (let j = 0; j < battleplans[i].length; j++) {
        if (j == 0) {
            var name = battleplans[i][j];
            document.getElementById("BP options").innerHTML += "<input type=\"radio\" id=\"" + name + "\" name=\"battleplan\" value=\"" + name + "\"></input>";
            document.getElementById("BP options").innerHTML += "<label for=\"" + name + "\">" + name + "</label>";
        } else {
            document.getElementById("BP options").innerHTML += "<p>";
            for (k = 0; k < battleplans[i][j].length; k++) {
                document.getElementById("BP options").innerHTML += battleplans[i][j][k] + "<br>";
            }
            document.getElementById("BP options").innerHTML += "</p>";
        }
    }
    document.getElementById("BP options").innerHTML += "<hr>";
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM