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> = [];, my goal is to check if new applicant doesn't have the same id as any of users that are already present in array.

Here is my model for GroupUser:

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:

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.

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. I think some() method can help but I'm not sure how to use it correct here.

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.

Upvotes: 0

Views: 284

Answers (3)

Barremian
Barremian

Reputation: 31145

You could use array some() in the following way

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

Upvotes: 1

Martin Zeitler
Martin Zeitler

Reputation: 76869

id is the name of the field ...which is never being compared to.

adminId probably should be userId, for the sake of readability.

While frankly speaking, just sort it on the server-side already.

Upvotes: 0

Rick
Rick

Reputation: 1880

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.

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.

Upvotes: 1

Related Questions