localStoragegetItemexamHistory的值为642023 103804000000001642023 1055022000000006。我运行以下JavaScript代码出现报错:Cannot read properties of undefined reading 0这是什么原因?如何解决?代码如下:examHistory = JSONparselocalStoragege
原因是在while循环中,条件应该是i < examHistory.length,而不是i <= examHistory.length。因为数组的下标从0开始,所以当i等于数组长度时,实际上已经超出了数组的范围,所以会出现undefined的情况。修改代码如下:
examHistory = JSON.parse(localStorage.getItem("examHistory")); var i = 0; while (i < examHistory.length) { data1 = examHistory[i][0].toString(); data2 = examHistory[i][1].toString(); data3 = examHistory[i][2].toString(); console.log(data2); content += "
原文地址: https://www.cveoy.top/t/topic/fQTz 著作权归作者所有。请勿转载和采集!