javascript:访问数组的数组内的项
我是Javascript新手,访问数组中的项时遇到问题。 我正在使用AngularJs框架,下面是代码:javascript:访问数组的数组内的项,javascript,Javascript,我是Javascript新手,访问数组中的项时遇到问题。 我正在使用AngularJs框架,下面是代码: $scope.db.items4 = []; var newRow={ ID:0, action1:[0,0,0,0,0,0,0], action2:[0,0,0,0,0,0,0] }; $scope.db.items4.push(newRow); for (var j = 0; j < 50; j++){ var lastRow=item
$scope.db.items4 = [];
var newRow={
ID:0,
action1:[0,0,0,0,0,0,0],
action2:[0,0,0,0,0,0,0]
};
$scope.db.items4.push(newRow);
for (var j = 0; j < 50; j++){
var lastRow=items4.length-1;
var thatDay=ts.items[j].day;
if(items4[lastRow].ID=="0"){
items4[lastRow]=ts.items[j].ID;
items4[lastRow].action1[thatDay]=ts.items[j].action1;
items4[lastRow].action2[thatDay]=ts.items[j].action2;
}else{
if(items4[lastRow].ID==ts.items[j].ID && items4[lastRow].action2[thatDay]=="0") {
items4[lastRow].action1[thatDay]=ts.items[j].action1;
items4[lastRow].action2[thatDay]=ts.items[j].action2;
} else{
var newRow2={
ID:0,
action1:[0,0,0,0,0,0,0],
action2:[0,0,0,0,0,0,0]
};
$scope.db.items4.push(newRow2);
lastRow++;
items4[lastRow]=ts.items[j].ID;
items4[lastRow].action1[thatDay]=ts.items[j].action1;
items4[lastRow].action2[thatDay]=ts.items[j].action2;
}
}
}
但很明显,项目4在一开始就已经定义好了;(感谢您的帮助。如果您想简化它,请将第一行更改为:
var item4 = $scope.db.items4 = [];
如果缓存
items4[lastRow]
您将使代码更简洁1000倍。不,项4
尚未定义,$scope.db.items4
已定义。是的,看起来您实际上从未定义过项4…这类似于var items4=$scope.db.items4;
讽刺的是,您已经键入项4一百万次,甚至忘记定义it、 谢谢。我以为$scope.db.items4表示items4。现在没有更多的“items4未定义”,但结果仍然不正确。items4[I].action2[j]是访问数组内元素的正确方法吗?谢谢。您正在使用ID值覆盖items4[lastRow],我希望它是字符串或数值:>>items4[lastRow]=ts.items[j].ID;因此现在items4[lastRow]包含ID的字符串或数字值,而不是对象。因此调用item4[lastRow].action1应该会引发错误。
var item4 = $scope.db.items4 = [];