无法读取未定义Javascript的属性"push" [英] Cannot read property 'push' of undefined Javascript

查看:422
本文介绍了无法读取未定义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()

这篇关于无法读取未定义Javascript的属性"push"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆