无法读取未定义Javascript的属性"push"

问题描述:

它就像我无法推动阵列一样? 代码:

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);

  });

我收到此控制台错误

Uncaught TypeError: Cannot read property 'push' of undefined

如果我设置了$scope.arrResult[item.week].push(item); to $scope.arrResult[item.week] = item; 它没有错误地工作 但是我需要/想要推动,怎么了?

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 ?

这是因为

$scope.arrResult[item.week]

它本身不是数组.

push()是的Array.prototype

要了解我的意思,请尝试

To see what I mean, try

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

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

,然后尝试push()