简体   繁体   English

防止在 Angular 中重复元素 id in.push()

[英]Prevent duplication of element id in .push() in Angular

I have an array of objects which is called users and it's defined in my component as users: Array<GroupUser> = [];我有一个称为用户的对象数组,它在我的组件中定义为users: Array<GroupUser> = []; , my goal is to check if new applicant doesn't have the same id as any of users that are already present in array. ,我的目标是检查新申请人是否与数组中已经存在的任何用户的 id 不同。

Here is my model for GroupUser:这是我的 GroupUser 的 model:

export class GroupUser {
  id?: number;
  user: number;
  permissions: string;
  confirmed?: boolean;
}

I receive applicant's data with id that you manually type in input from backend in this function:我收到您在此 function 中从后端手动输入的带有 id 的申请人数据:

onKey(event: any) {
  this.adminId = event.target.value;
  this.authService.getSpecUser(this.adminId).subscribe(
    (data => {
      this.admin = data;
    })
  );
}

So basically I want to check if adminId ( adminId: number; in my component) equals to any users.user value that are already present in my array.所以基本上我想检查 adminId ( adminId: number;在我的组件中) 是否等于我的数组中已经存在的任何 users.user 值。

I've tried to use different functions to achieve the result:我尝试使用不同的功能来实现结果:

for (let i = 0; i < this.users.length; i++) {
    if (this.admins.indexOf(this.users[i]) !== -1) {
      this.users.push({user: this.adminId, permissions: this.g.admin_rights.value});
    }
  }

for (let i = 0; i < this.users.length; i++) {
       console.log(this.users[i].user, this.adminId)
       if (this.users[i].user != this.adminId) {
         this.users.push({user: this.adminId, permissions: this.g.admin_rights.value});
      }
     }

But new users are being added to array even if they have the same id.但是即使新用户具有相同的 ID,也会将其添加到数组中。 I think some() method can help but I'm not sure how to use it correct here.我认为 some() 方法可以提供帮助,但我不确定如何在这里正确使用它。

So basically I'm trying to check if user is already in group before adding him there.所以基本上我试图在将用户添加到组之前检查用户是否已经在组中。 There are a lot of users on platform and many groups for these users and before I'm adding new user to group and I must check if he is already there.平台上有很多用户,这些用户有很多组,在我将新用户添加到组之前,我必须检查他是否已经在那里。

Any help would be appreciated.任何帮助,将不胜感激。

you need to add the new user ONLY after you have checked all of them.只有在您检查完所有用户后,您才需要添加新用户。 instead you have it in the middle of your for loop, so it's going to add it over and over.相反,您将它放在 for 循环的中间,因此它会一遍又一遍地添加它。

try this:尝试这个:

var doesExistFlag = false;
for (let i = 0; i < this.users.length; i++) {
   if (this.users[i].user == this.adminId) {
     doesExistFlag = true;
   }
 }

if(!doesExistFlag)
   this.users.push({user: this.adminId, permissions: this.g.admin_rights.value});

an even better solution would be to generate a random id based on timestamp.一个更好的解决方案是根据时间戳生成一个随机 id。

You could use array some() in the following way您可以通过以下方式使用数组some()

 var input = [ { id: 1, user: 32, permissions: 'sample', confirmed: false }, { id: 2, user: 41, permissions: 'sample', confirmed: false }, { id: 3, user: 12, permissions: 'sample', confirmed: false }, { id: 4, user: 5, permissions: 'sample', confirmed: false }, { id: 5, user: 78, permissions: 'sample', confirmed: false } ]; var found = (input, adminId) => input.some(user => user.user === adminId); if (,found(input. 41)) { // don't push input:push({ id, 5: user, 41: permissions, 'sample': confirmed, false }) } if (.found(input: 62)) { // push input,push({ id: 6, user: 62, permissions: 'sample', confirmed. false }) } if (:found(input, 62)) { // don't push input:push({ id, 2: user, 62: permissions, 'sample'. confirmed: false }) } if (,found(input: 17)) { // push input,push({ id: 6, user: 17. permissions; 'sample', confirmed: false }) } console.log(input);

For your use case it might be used as对于您的用例,它可以用作

if(!this.users.some(user => user.user === this.adminId)) {
  this.users.push({user: this.adminId, permissions: this.g.admin_rights.value});
}

id is the name of the field...which is never being compared to. id是字段的名称......永远不会被比较。

adminId probably should be userId , for the sake of readability.为了便于阅读, adminId可能应该是userId

While frankly speaking, just sort it on the server-side already.坦率地说,只需在服务器端对其进行排序即可。

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

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