数组迭代出现“未捕获类型错误:无法读取未定义的属性‘长度’”
2020-11-24
112
我有一个简单的三层数组,我每次迭代一位。出于某种原因,在对所有内容进行迭代后,它会出现“未捕获的类型错误:无法读取未定义的属性‘长度’”错误。
有问题的代码是:
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 循环中取出迭代以确保它不会尝试迭代不存在的内容,它会错过一些位但仍然会出现错误。
我设法找到了一些类似的问题,这些问题要求检查目标是否已定义,但代码并没有告诉它检查任何它尚未检查并发现存在的内容(我已经通过将 length 属性的值输出到控制台来验证了这一点)。它似乎只是在完成所有操作之后触发错误。
除此之外,我不太了解其他人的代码,无法看到与我的代码的任何相似之处。
任何建议都将不胜感激。
编辑:根据要求提供更多信息。
这是 battleplans 常量:
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>";
}
1个回答
你的 for 循环有错误 在第二个循环中将 i < battleplans[i].length; 更改为 j < battleplans[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>";
}
Nonik
2020-11-25