0

i'm working on a .forEach loop of users in jQuery and i want to create an object array of the user groups to use for something else. rather then pull the data in for all users again for the function

here is my example below

   var data = [
                    {
                            "user_id": '1',
                            "username": "test2",
                            "email": "test@test.com",
                            "access_type": "admin"
                    },
                    {
                            "user_id": '2',
                            "username": "test2",
                            "email": "test2@test2.com",
                            "access_type": "admin"
                    },                    {
                            "user_id": '3',
                            "username": "test3",
                            "email": "test3@test3.com",
                            "access_type": "user"
                    }
                ];


      var users_select = {};

   data.forEach(function(i){
      
    var type = i.access_type;
    var setup = users_select;
                var fullcount = setup.type.typecount;
    if(fullcount !== undefined){
     fullcount =  fullcount + 1;
    } else {
     setup[type] = {'name':type,'typecount':0};
    }

      });

      console.log(users_select);
 //example of data for the for each loop

i get the error "Uncaught TypeError: Cannot read property 'typecount' of undefined" the if statement should be enough to setup the object

the desired output should look like this

{ admin: {
    name: "admin",
    typecount: 2
  },
  user: {
    name: "admin",
    typecount: 1
  }
}
c.naka
  • 27
  • 2

0 Answers0