Home Two dimensional array error with push()
Reply: 0

Two dimensional array error with push()

user2074
1#
user2074 Published in April 27, 2018, 8:31 am

I'm trying to sort db data by groupID(different groupID in different sub-array) but when I trying to push it into array I see this error

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: {}"

This is my code:

$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;
                        }
                    }
                }

This is what response look like:

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}
You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.324847 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO