Unable to read the "push" property from undefined Javascript

Hi, does it look like I cannot push an array? the 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 installed $scope.arrResult[item.week].push(item); to $scope.arrResult[item.week] = item; it works without errors but I need / need to click, what's wrong?

+4
source share
1 answer

It's because

$scope.arrResult[item.week]

is not itself an array.

push () hasArray.prototype

To understand what I mean, try

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

or

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

and then try push()

+9
source

Source: https://habr.com/ru/post/1588997/


All Articles