Javascript 如何在检查另一个数组后向数组添加值

Javascript 如何在检查另一个数组后向数组添加值,javascript,angularjs,arrays,ecmascript-6,Javascript,Angularjs,Arrays,Ecmascript 6,我有两个这样的阵列: UserGroupUser[{Id:"1",UserId:"2",UserGroupId"1"}, {Id:"2",UserId:"3",UserGroupId"1"}, {Id:"3",UserId:"4",UserGroupId"2"}] UserGroupId将具有1、2、3等值 Employee[{EmployeeId:"1", EmpName:"John", Email:"john@example.com"},

我有两个这样的阵列:

UserGroupUser[{Id:"1",UserId:"2",UserGroupId"1"},
           {Id:"2",UserId:"3",UserGroupId"1"},
            {Id:"3",UserId:"4",UserGroupId"2"}]
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"}   ]
我将在变量GroupId中存储一个数字,例如GroupId=1 我要做的是检查UserGroupUser表,如果GroupId 1匹配关键UserGroupId的任何行,并且如果每个UserId都匹配,那么Employee表中匹配的对应EmployeeId意味着我添加了一个名为registed=true的新元素。否则,如果没有匹配项,则将元素添加到Employee Registered=false

例如:

如果GroupId=1,那么我想在UserGroupUser数组中获取UserGroupId为1的用户的userId,并将registed:true添加到Employee数组EmployeeId中,与userId对应

我就是这样做的

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

                    }
                });
我做错了什么?我该怎么做呢?

试试这个

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);
然后,员工将在您的控制台中包含已注册或未注册的信息,也可以尝试此操作

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

然后,员工将在您的控制台中包含已注册的或未尝试此操作

您的代码的问题是当
if(arrayItem.UserGroupId===GroupId){
被执行时,它将相关员工的
登记
更改为
真实
,但当执行此检查的
其他
部分时,它将覆盖
如果
条件部分所做的更改。 试试这个

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

代码的问题在于,当执行
if(arrayItem.UserGroupId===GroupId){
时,相关员工的
registered
更改为
true
,但当执行此检查的
else
部分时,它会覆盖
if
条件部分所做的更改。 试试这个

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


您不能从数组返回。forEach()我想您需要通过添加registered=true/false来更改数组employee,不是吗?返回来自map()吗函数,该函数将新更改的员工数组分配给结果。是的…如果该用户在GroupId=1下的UserGroupUser数组中,请等待1分钟,让我创建无法从数组返回的Fiddle。forEach()我想您需要通过添加registed=true/false来更改数组员工。返回来自映射()函数,该函数将新更改的员工数组分配给结果。是…如果该用户在GroupId=1下的UserGroupUser数组中,请等待1分钟,让我创建Fiddle。我将获得一个变量GroupId的值。我必须将该值与userGroup.UserGroupId匹配,然后仅对该组下的用户进行更改我在中未看到组id员工列表您可以添加他们吗?然后我将更新AnswreWhere is not。基本上,我想在userGroup表中的groupId=1的员工上更改employee表中的已注册:true…因此,如何知道哪个员工在groupId=1中,而员工列表中没有属性。例如,我需要使用一些值进行检查。基本上,来自employee表将被添加到一个组中。然后他们将被添加到UserGroup表中,其employeeNo作为UserId,并且他们属于哪个组。即groupId=1。然后我将继续搜索特定groupId下的所有用户,并将employee表中的那些用户更改为registed:true。当我说特定的组id时,这是s作为参数给出。因此它可以更改。我将获取变量GroupId的值。我必须与userGroup.UserGroupId匹配,然后仅对该组下的用户进行更改。我在员工列表中未看到组id。您可以添加这些用户吗?然后我将更新Answer。基本上,我想更改已注册:在员工列表中为trueyee表中位于userGroup表中groupId=1的员工…因此,如何知道哪些员工位于Id=1的组中,而员工列表中没有属性。例如,我需要检查一些值。基本上,员工表中的员工将添加到组中。然后,他们将添加到userGroup表中,其employeeNo作为用户Id and他们所属的组..即groupId=1。然后我将搜索特定groupId下的所有用户,并在employee表中使用registered:true更改这些用户。当我说特定组id时,这是一个参数..因此它可以更改。。