push()的二维数组错误

时间:2018-01-12 15:06:50

标签: javascript mysql arrays angularjs

我试图按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}

1 个答案:

答案 0 :(得分:0)

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;
}, {});

试一试,看看它是如何工作的! (如果您有任何疑问,请继续询问)

编辑一个工作片段:

&#13;
&#13;
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);
&#13;
&#13;
&#13;