Vincent Zhou
Vincent Zhou

Reputation: 503

javascript: access the item inside array of array

I'm new to Javascript and I have a problem to access the item inside array of array. I'm using the AngularJs framework and here is the code:

$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;
            }
        }
   }

When I run it, the javascript console always says:

Uncaught ReferenceError: items4 is not defined 

But obviously items4 has been defined in the beginning; ( any help is appreciated.

Upvotes: 0

Views: 129

Answers (1)

Ahmed El Kilani
Ahmed El Kilani

Reputation: 345

If you wanna simplify it change the first line to be like this:

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

Upvotes: 1

Related Questions