Uncaught TypeError错误求助:Cannot read properties of undefined (reading 'includes')
问题排查:Uncaught TypeError: Cannot read properties of undefined (reading 'includes')
我始终遇到Uncaught TypeError: Cannot read properties of undefined (reading 'includes')错误,报错位置为script.js:35:21。移除代码中的j++后错误不再出现,但不清楚具体原因,请求排查。相关代码如下:
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被双重递增:外层for循环已经自带j++的自增逻辑,循环体内部又额外写了一次j++,导致j每次循环都会增加2,增长速度直接翻倍。 - 数组越界触发报错:
curShelf(也就是shelf1)只有10个元素,索引范围是0到9。当j快速涨到10时,curShelf[j]就会变成undefined,此时调用undefined.includes()必然抛出类型错误。 - 循环终止条件完全无效:
test !== []这个判断永远为真,因为数组是引用类型,test和空数组[]是两个不同的引用,循环根本不会自动停止,直到j超出数组索引范围触发报错。
修复方案
- 删掉重复的
j++:只保留外层for循环里的j++,避免j过度增长。 - 修正循环终止条件:用
test.length > 0代替test !== [],当test数组被清空时自动终止循环。 - 可选:保护原数组:如果不想改动
shelf1的原始数据,赋值时可以做深拷贝,比如curShelf = JSON.parse(JSON.stringify(shelf1))。
修复后的代码示例
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); // 深拷贝避免修改原shelf1数据 let curShelf = JSON.parse(JSON.stringify(shelf1)); console.log(curShelf); let i = 0; // 用test.length > 0作为终止条件,数组为空时停止循环 for (let j = 0; test.length > 0; j++) { for (i = 0; i < testCase.length; i++) { // 先判断curShelf[j]是否存在,防止越界报错 if (curShelf[j] && 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); } // 原代码中count未定义,这里补充默认值避免报错 const distance = (typeof count !== 'undefined' ? count : 0) + j; console.log("cock"); console.log("Total distance: ", distance);
内容的提问来源于stack exchange,提问作者John Lamb
相关产品推荐
相关产品推荐

