无法读取未定义 Javascript 的属性“push”[重复]

2023-12-21

您好,我似乎无法推送我的阵列? 代码:

  $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;它工作没有错误 但我需要/想要推动,怎么了?


这是因为

$scope.arrResult[item.week]

本身不是一个数组。

push() https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/push is of Array.prototype

要明白我的意思,请尝试

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

or

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

然后尝试push()

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

无法读取未定义 Javascript 的属性“push”[重复] 的相关文章

随机推荐