未捕获的类型错误:无法读取未定义的属性(读取"包含")?



我不明白为什么我一直得到这个错误。请帮助。

Uncaught TypeError: Cannot read properties of undefined (reading 'includes') at script.js:35:21

const testCase = ["A", "A", "C", "D"];
const shelf1 = [
["A", "B", "C", "D"],
["E", "F", "G", "H"],
["I", "J", "A", "B"],
["C", "D", "E", "F"],
["G", "H", "I", "J"],
["A", "B", "C", "D"],
["E", "F", "G", "H"],
["I", "J", "A", "B"],
["C", "D", "E", "F"],
["G", "H", "I", "J"],
];
const shelf2 = [
["A", "A", "A"],
["B", "B", "B"],
["C", "C", "C"],
["D", "D", "D"],
["E", "E", "E"],
["F", "F", "F"],
["G", "G", "G"],
["H", "H", "H"],
["I", "I", "I"],
["J", "J", "J"],
];
const test = testCase.slice();
console.log(test);
let curShelf = [];
curShelf = shelf1; // shelf1, shelf2
console.log(curShelf);
let i = 0;
for (let j = 0; test !== []; j++) {
for (i = 0; i < testCase.length; i++) {
if (curShelf[j].includes(testCase[i])) {
let index1 = test.indexOf(testCase[i]);
let index2 = curShelf[j].indexOf(testCase[i]);
if (index1 > -1 && index2 > -1) {
test.splice(index1, 1);
curShelf[j].splice(index2, 1);
}
}
}
j++; 
console.log(test, j);
}
const distance = count + j;
console.log("cock");
console.log("Total distance: ", distance);

当我试图删除j++时,它不会出现,但我只是无能为力。

你不应该使用test != [],因为数组是通过引用&你的条件总是true,因为它每次都比较一个新的参考。相反,使用test.length > 0这样的条件,您必须迭代直到数组结束。

最新更新