【问题标题】:Two dimensional array error with push()push() 的二维数组错误
【发布时间】:2018-01-12 15:06:50
【问题描述】:

我正在尝试按groupID(不同的子数组中的不同groupID)对数据库数据进行排序,但是当我尝试将其推入数组时,我看到了这个错误

TypeError: Cannot read property 'push' of undefined
at app.js:30
at angular.min.js:136
at m.$digest (angular.min.js:147)
at m.$apply (angular.min.js:151)
at l (angular.min.js:103)
at s (angular.min.js:108)
at XMLHttpRequest.y.onload (angular.min.js:109) "Possibly unhandled rejection: {}"

这是我的代码:

$scope.tasks = [[]];
                var temp = [];
                var k = 0;
                $scope.tempTasks = response.data;
                for (var i = 0; $scope.tempTasks[i]; i++) {
                    if (k > 0) {
                        $scope.tasks.push( [] );
                    }
                    $scope.tasks[k].push($scope.tempTasks[k]);

                    for (var j = 0; j < $scope.tempTasks.length; j++){              
                        if($scope.tempTasks[i].groupID == $scope.tempTasks[j].groupID){
                            $scope.tasks[i].push($scope.tempTasks[j]);
                        } else {
                            temp[k] = j;
                            k++;
                            continue;
                        }
                    }
                }

这是response 的样子:

data: Array(7)
   0: {ID: 2, Title: "Don't do it", Description: "Just don't do it!", Date: "2018-01-10", groupID: 1}
   1: {ID: 3, Title: "Do it", Description: "Just do it", Date: "1999-12-31", groupID: 3}
   2: {ID: 4, Title: "asd", Description: "asd", Date: "2000-10-10", groupID: 2}
   3: {ID: 6, Title: "jnjkhjkkjh", Description: "jihjuiuiu", Date: "78-9809-99", groupID: 3}
   4: {ID: 7, Title: "asdasd", Description: "asdasdasd", Date: "2000-10-10", groupID: 4}
   5: {ID: 12, Title: "asd", Description: "asd", Date: "2000-10-10", groupID: 4}
   6: {ID: 13, Title: "asd", Description: "asd", Date: "2000-10-10", groupID: 2}

【问题讨论】:

    标签: javascript mysql arrays angularjs


    【解决方案1】:

    Javascript 有一个 reduce 函数。

    这是您使用该功能的代码:

    const data = response.data;
    const groupedData = data.reduce((previous, next) => {
      if(previous[next.groupID]) { previous[next.groupID].push(next); }
      else { previous[next.groupID] = [next]; }
      return previous;
    }, {});
    

    试试看,看看效果如何! (如果您有任何问题,请继续提问)

    编辑一个工作的sn-p:

    const data = [{ ID: 2, Title: "Don't do it", Description: "Just don't do it!", Date: "2018-01-10", groupID: 1 },
      { ID: 3, Title: "Do it", Description: "Just do it", Date: "1999-12-31", groupID: 3 },
      { ID: 4, Title: "asd", Description: "asd", Date: "2000-10-10", groupID: 2 },
      { ID: 6, Title: "jnjkhjkkjh", Description: "jihjuiuiu", Date: "78-9809-99", groupID: 3 },
      { ID: 7, Title: "asdasd", Description: "asdasdasd", Date: "2000-10-10", groupID: 4 },
      { ID: 12, Title: "asd", Description: "asd", Date: "2000-10-10", groupID: 4 },
      { ID: 13, Title: "asd", Description: "asd", Date: "2000-10-10", groupID: 2 }
    ];
    
    const groupedData = data.reduce((previous, next) => {
      if (previous[next.groupID]) { previous[next.groupID].push(next); } 
      else { previous[next.groupID] = [next]; }
      return previous;
    }, {});
    
    console.log(groupedData);

    【讨论】:

    • TypeError: Cannot read property '3' of undefined in if(previous[next.groupID]) { previous[next.groupID].push(next); }
    • 是的,我复制粘贴了
    • 好的,我现在正在开车,但是半小时后我回家后会做一个工作的sn-p!
    • 非常感谢,它会救我的)
    • 我是个笨蛋 :D 没有代码完成我一文不值......你只需要在 else 之后添加 return previous !我编辑了我的帖子并发布了一个 sn-p,以防你想尝试
    猜你喜欢
    • 2012-07-05
    • 2021-03-24
    • 2010-09-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多