0

I have two arrays as such :

UserGroupUser[{Id:"1",UserId:"2",UserGroupId"1"},
           {Id:"2",UserId:"3",UserGroupId"1"},
            {Id:"3",UserId:"4",UserGroupId"2"}]

UserGroupId will have values such as 1, 2, 3 etc.

Employee[{EmployeeId:"1", EmpName:"John", Email:"[email protected]"},
       {EmployeeId:"2", EmpName:"Mary", Email:"[email protected]"},
         {EmployeeId:"3", EmpName:"Sarah", Email:"[email protected]"},
         {EmployeeId:"4", EmpName:"Jake", Email:"[email protected]"}   ]

I will store a number in a variable GroupId such as GroupId=1 and what i want to do is check the UserGroupUser table if GroupId 1 matches any rows for key UserGroupId and if there is a match for every UserId the corresponding EmployeeId in Employee table that matches would mean i add a new element called enrolled=true. else if there is not match add a element to Employee enrolled=false.

for eg:

If GroupId is =1 then i want to get the userId of those with the UserGroupId as 1 in the UserGroupUser array and add enrolled:true into the Employee array EmployeeId to those corresponding to the UserId .

This is how i tried to do it..

 UserGroupUser.forEach(function (arrayItem) {
                    if (arrayItem.UserGroupId === GroupId) {
                        result = Employee.map(function (a, index, array) {
                            while (arrayItem.UserId === a.EmployeeNo) {

                                a.enrolled = true;
                            }

                            return a;
                        }
                        );
                    }
                    else {
                        result = Employee.map(function (a, index, array) {
                            a.enrolled = false;
                            return a;
                        }
                        );

                    }
                });

what am i doing wrong? how should i do this?

8
  • you cannot return from Array.forEach() Commented May 4, 2017 at 5:15
  • i think you need to change the array employee by adding enrolled = true/false isnt it Commented May 4, 2017 at 5:16
  • The return is from the map() function which will assign the new changed Employee array to result. Commented May 4, 2017 at 5:17
  • yes... if that user is in the UserGroupUser array under GroupId = 1 Commented May 4, 2017 at 5:17
  • Okay 1 min let me create the fiddle Commented May 4, 2017 at 5:18

2 Answers 2

1

Try this

var userGroup = [{Id:"1",UserId:"2",UserGroupId:"1"},
           {Id:"2",UserId:"3",UserGroupId:"1"},
            {Id:"3",UserId:"4",UserGroupId:"2"}]

var employees = [{EmployeeId:"1", EmpName:"John", Email:"[email protected]"},
       {EmployeeId:"2", EmpName:"Mary", Email:"[email protected]"},
         {EmployeeId:"3", EmpName:"Sarah", Email:"[email protected]"},
         {EmployeeId:"4", EmpName:"Jake", Email:"[email protected]"}   ]

employees.forEach(function(item){
        var found = userGroup.filter(i=>i.UserId==item.Id);
        if(found.length>0)
            item.enrolled = true
        else
            item.enrolled = false
})

console.log(employees);

the employees then will contained the enrolled or not try this in your console too

Sign up to request clarification or add additional context in comments.

7 Comments

I will be getting a value to a variable GroupId. This i must match with userGroup.UserGroupId and then do the changes to only those users under that group
i havnt seen group id in employees list can you add them Then i will update the answre
there isnt. Basically I want to change enrolled : true in employee table on those employees that are in groupId =1 in the userGroup table...
so how to know which employee is in groud Id =1 without a property in employee list. ie i need to check with some value.
Basically, a employee from the employee table will be added to a group. Then they will be added to the UserGroup table with their employeeNo as UserId and to which group they belong.. i.e groupId=1. Then i would go about searching for all the users under a specicfic groupId and changed those users in the employee table with enrolled : true . when i say specific group id this is given as a argument .. so it can change..
|
1

The problem with your code is that when if (arrayItem.UserGroupId === GroupId) { is executed, it changes enrolled to true for the concerned employees but when the else part of this check is executed, it overrides the changes made by the if condition part. Try this.

UserGroupUser = [{Id:"1",UserId:"2",UserGroupId:"1"},
             {Id:"2",UserId:"3",UserGroupId:"1"},
             {Id:"3",UserId:"4",UserGroupId:"2"}];
Employee = [{EmployeeId:"1", EmpName:"John", Email:"[email protected]"},
        {EmployeeId:"2", EmpName:"Mary", Email:"[email protected]"},
        {EmployeeId:"3", EmpName:"Sarah", Email:"[email protected]"},
        {EmployeeId:"4", EmpName:"Jake", Email:"[email protected]"}];
GroupId = "1";
Employee.map(function (emp) {
  emp.enrolled = false;
});
UserGroupUser.forEach(function (arrayItem) {
  if (arrayItem.UserGroupId === GroupId) {
    Employee.map(function (emp) {
      if (arrayItem.UserId === emp.EmployeeId) {
        emp.enrolled = true;
      }
    });
  }
});
console.log(Employee);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.