2013-04-24 38 views
0

我是新来的Javascript和我有一个问题,以访问阵列内的数组内的项目。 我使用AngularJs框架,这里是代码:javascript:访问阵列内的项目

$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 

但显然items4已开始被定义; (任何帮助表示赞赏

+3

如果缓存'items4 [LASTROW]'你会让代码1000次清洁 – elclanrs 2013-04-24 20:59:06

+5

不,'items4'没有被定义,'$ scope.db.items4'已经被使用。 – 2013-04-24 21:00:59

+0

是的,它看起来像你实际上从来没有定义过items4 ...这就像'var items4 = $ scope.db .items4;' – Timmerz 2013-04-24 21:02:02

回答

1

如果你想简化它改变的第一行是这样的:。

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

Thanks.I认为$ scope.db.items4意味着items4。现在没有更多的“items4未定义”,但是结果仍然不正确。是items4 [i] .act ion2 [j]访问数组中的元素的正确方法?谢谢。 – 2013-04-25 00:35:06

+0

嗯,你正在覆盖物品4 [lastRow]的ID值,我认为它是一个字符串或数字值:>> items4 [lastRow] = ts.items [j] .ID; 所以现在items4 [lastRow]包含一个字符串或数字值的ID而不是对象。所以调用item4 [lastRow] .action1应该会引发错误。 – 2013-04-25 17:53:56