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

Two dimensional array error with push()

BXD
1#
BXD Published in 2018-01-12 15:06:50Z

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}
trichetriche
2#
trichetriche Reply to 2018-01-12 16:20:28Z

Javascript has a reduce function for that.

Here is your code with that function :

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

Try it, see how it works ! (and if you have any question, go ahead and ask it)

EDIT a working snippet :

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);

You need to login account before you can post.

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

© 2016 Powered by mzan.com design MATCHINFO