You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

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超出数组索引范围触发报错。
修复方案
  1. 删掉重复的j++:只保留外层for循环里的j++,避免j过度增长。
  2. 修正循环终止条件:用test.length > 0代替test !== [],当test数组被清空时自动终止循环。
  3. 可选:保护原数组:如果不想改动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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.05 23:40:18