Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot read property 'push' of undefined Javascript [duplicate]

Hi it seams like i can't push my array ? code:

  $scope.arrResult = [];
  dpd.timesheets.get( function (result) {
    console.log(result);

    for (i = 0, n = result.length; i < n; i++) {
      var item = result[i];
      $scope.arrResult[item.week].push(item);
    }
    console.log($scope.arrResult);

  });

i get this console error

Uncaught TypeError: Cannot read property 'push' of undefined

if i set $scope.arrResult[item.week].push(item); to $scope.arrResult[item.week] = item; it works with no error but i need / want to push, whats wrong ?

like image 702
Stweet Avatar asked May 20 '15 13:05

Stweet


1 Answers

This is because

$scope.arrResult[item.week]

itself is not an array.

push() is of Array.prototype

To see what I mean, try

$scope.arrResult[item.week] = [];

or

$scope.arrResult[item.week] = new Array();

and then try push()

like image 110
AmmarCSE Avatar answered Sep 29 '22 09:09

AmmarCSE