我正在尝试使用数组来获取特定结果,但出现错误无法读取未定义的属性,但是当我控制台记录相同的属性时,我得到的结果是我的代码:
for (var j=0;j<$scope.ftListe.length;j++){
if(task[j].projet_id==id){
if(temp!=task[j].NomTache)
temptache.push(task[j]);
tempcol.push(lecollaborateur[j]);
}
temp=task[j].NomTache;
}
$scope.temptache=temptache;
$scope.tempcol=tempcol;
for(var i=0; i<temptache.length;i++){
for (var k=0;k<$scope.ftListe.length;i++){
console.log("l'id de la tache: "+temptache[i].IdTache);
if(temptache[i].IdTache==$scope.ftListe[k].tacheid){
temps = temps+$scope.ftListe[k].TempsPasse;
passe= passe+$scope.ftListe[k].TempsPasse * lecollaborateur[k].CoutParJour;
}
}
tempsAr.push(temps);
passeAr.push(passe);
temps=0; passe=0;
}
我从中得到错误的属性是 IdTache
,我做错了什么?
最佳答案
请看下面几行:
for(var i=0; i<temptache.length;i++){
for (var k=0;k<$scope.ftListe.length;i++){
请注意,在内部 for
循环中,您递增了 i
变量 (i++
) 而不是 k
.所以这使得 i
在内循环中获得增量到越界偏移量。
JS 运行时错误阻止您的脚本进入无限循环 - 所以当您声明 “当我控制台记录相同的属性时,我得到我的结果” 它实际上显示结果直到指向您在 temptache
数组的有效范围内的位置。
您应该将其修复为:
for(var i=0; i<temptache.length;i++){
for (var k=0;k<$scope.ftListe.length;k++){
关于javascript - 无法读取未定义错误的属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44244412/