简体   繁体   English

检查另一个数组后如何为数组添加值

[英]How to add value to array after checking another array

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. UserGroupId将具有诸如1、2、3等的值。

Employee[{EmployeeId:"1", EmpName:"John", Email:"john@example.com"},
       {EmployeeId:"2", EmpName:"Mary", Email:"Mary@example.com"},
         {EmployeeId:"3", EmpName:"Sarah", Email:"Sarah@example.com"},
         {EmployeeId:"4", EmpName:"Jake", Email:"Jake@example.com"}   ]

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. 我将在一个变量GroupId中存储一个数字,例如GroupId = 1,我要检查的是UserGroupUser表,如果GroupId 1匹配键UserGroupId的任何行,并且如果每个UserId都匹配,则Employee表中的对应EmployeeId匹配将意味着我添加了一个名为enrolled = true的新元素 else if there is not match add a element to Employee enrolled=false . 否则,如果不匹配,则向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 . 如果GroupId = = 1,那么我想在UserGroupUser数组中获取UserGroupId为1的用户的userId,并将edrolled:true添加到Employee数组EmployeeId中,以与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? 我应该怎么做?

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:"john@example.com"},
       {EmployeeId:"2", EmpName:"Mary", Email:"Mary@example.com"},
         {EmployeeId:"3", EmpName:"Sarah", Email:"Sarah@example.com"},
         {EmployeeId:"4", EmpName:"Jake", Email:"Jake@example.com"}   ]

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 然后员工将包含已注册的内容,或者也不会在您的控制台中尝试此操作

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. 您的代码的问题在于,当执行if (arrayItem.UserGroupId === GroupId) { ,它会将有关员工的enrolled更改为true ,但是当执行此检查的else部分时,它将覆盖if所做的更改。条件部分。 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:"john@example.com"},
        {EmployeeId:"2", EmpName:"Mary", Email:"Mary@example.com"},
        {EmployeeId:"3", EmpName:"Sarah", Email:"Sarah@example.com"},
        {EmployeeId:"4", EmpName:"Jake", Email:"Jake@example.com"}];
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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM