未捕获的类型错误:不能读取属性-尽管属性存在
Uncaught TypeError: Cannot read property - Though property exists
var sorted = DocumentListData.Documents.sort(function (a, b) {
var nameA = a.Document.toLowerCase(),
nameB = b.Document.toLowerCase();
return nameA.localeCompare(nameB);
});
for (var i = 0, len = sorted.length; i < len; i++) {
if (sorted[i].Document=== 'abc') {
sorted.splice(i, 1);
}
if (sorted[i].Document=== 'xyz') {
sorted.splice(i, 1);
}
}
我花了一个小时来绞尽脑汁想这里到底出了什么错。虽然Document属性存在,但是Document是undefined。
Uncaught TypeError: Cannot read property 'Document' of undefined
。当我移除sorted.splice(i,1)
时,它工作并且没有错误。
您正在修改要迭代的数组。splice(i,1)
移除第i个元素。当您拼接排序数组时,您正在删除元素,因此您最终会超过数组的长度,因为循环会到达数组的原始长度。然后sorted[i]
是未定义的,并且您得到您正在描述的错误。
一般来说,以这种方式修改当前正在迭代的数组绝对不是一个好主意。如果您必须这样做,请确保包含If检查,以查看i
不是>=
到数组的当前长度。但是在这里使用while循环可能更合适。
var counter =0;
while(counter < sorted.length) {
var doc = sorted[counter].Document;
if (doc === 'abc' ||doc === 'xyz') {
sorted.splice(counter, 1);
}
else{
counter++;
}
}
几点思考
document
是javascript浏览器环境中预定义的变量,通常JS有一个约定,大写的变量只用于构造函数。所以Document
可能不是一个很好的属性名选择。如果你不需要支持旧版本的IE,你也可以查看Array.Prototype.filter作为一种更干净的方式来做你想做的事情
如果最后一个元素的Document
是abc
,它将在第一个if
条件下被删除。
sorted[i]
将变成undefined
(因为你已经删除了最后一个元素),当第二个if
运行时,你会得到你所描述的错误。
你可以使用else if
:
if (sorted[i].Document=== 'abc') {
sorted.splice(i, 1);
} else if (sorted[i].Document=== 'xyz') {
sorted.splice(i, 1);
}
相关文章:
- TypeError:无法读取属性'推'未定义的JavaScript
- 同样,同样的错误'ahorcado.js:26未捕获类型错误:无法读取属性'beginPath'
- 未捕获的TypeError无法读取未定义的属性socialsharing
- 未捕获的类型错误:无法读取属性'删除'的未定义
- AngularJS指令出错-无法读取属性'编译'的未定义
- 未捕获的类型错误:无法读取属性'name'即使它存在,也无法定义
- 看到“;未捕获的类型错误:无法读取属性'weight'未定义的“;尽管按照字面上的指示
- angularjs无法读取未定义的属性then
- 无法读取属性'材料'未定义的Three.js
- 得到"TypeError:无法读取属性'filename'未定义的“;调用“npm start
- Soundcloud api"未捕获的类型错误:无法读取属性'uri'“未定义”;
- 未捕获的类型错误:无法读取属性'addEventListener'的null chrome扩展名
- Soundcloud Javascript SDK 3.0-回调无法读取属性'connectCallback
- 未捕获的类型错误:无法读取属性'状态'在react中为null
- JavaScript承诺-无法读取属性'那么'的未定义
- Javascript Float32数组抛出无法读取属性'0'即使数组定义良好,也为null
- 未捕获的类型错误:无法读取属性'中止'的未定义
- 角度推入数组给出:TypeError:无法读取属性'推'的未定义
- http/rxjs catch回调中的Angular 2重定向导致TypeError:无法读取属性'订阅'
- 角度输入模型有条件地从一个属性读取,写入另一个属性