javascript:访问数组数组中的项

时间:2013-04-24 20:58:24

标签: 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=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;
            }
        }
   }

当我运行它时,javascript控制台总是说:

Uncaught ReferenceError: items4 is not defined 

但很明显,item4已经定义了; (感谢任何帮助。

1 个答案:

答案 0 :(得分:1)

如果您想简化它,请将第一行更改为:

var item4 = $scope.db.items4 = [];