0

I am doing an APP in Angular 12. In my component.ts I have an string array[] like

AccessRoles["AA","BB"]

And on the other side I have a class like this

export class UserRole {
  Id :string;
  Active:boolean;
}

My class have this value UserRole

  "UserRole": [
        {
          "id": "AA",
          "Active": "false"
    
        },
        {
          "id": "BB",
          "Active": "false"
        },
  {
          "id": "CC",
          "Active": "false"
        },
]

I need to set Active = true when element in UserRole is in AccessRoles

I can do it using nested foreach, but is has low performance.

What I mean by bad performance, is the bad practice of using nested ForEach. Any solution that does not use nested ForAEach is acceptable.

As is mentioned as an answer,here it helps, but I need to have my object with the same count of records.. If I filter, I will loose those records where is Active is false.

What I need as answer is like this

 "UserRole": [
            {
              "id": "AA",
              "Active": "true"
        
            },
            {
              "id": "BB",
              "Active": "true"
            },
      {
              "id": "CC",
              "Active": "false"
            },
    ]

Thanks

Diego
  • 2,238
  • 4
  • 31
  • 68
  • Please [edit] your question to show what measure of performance is low and what level would not be low. There are many solutions for this, many of which are already present on this site, and we'd hate to give you one with low performance. – Heretic Monkey May 05 '22 at 21:13
  • Heretic, I just explained what I mean... what I mean by bad performance, is the bad practice of using nested ForEach. Any solution that does not use nested ForAEach is acceptable. Thanks – Diego May 05 '22 at 21:20
  • 3
    Does this answer your question? [How to filter array when object key value is in array](https://stackoverflow.com/questions/35817565/how-to-filter-array-when-object-key-value-is-in-array) – Heretic Monkey May 05 '22 at 21:23
  • The dupe shows how to filter the array to those objects where the user role is in access roles. Then you only need one "ForEach" to update the property. – Heretic Monkey May 05 '22 at 21:25
  • Upvoted @HereticMonkey 's suggestion. That's literally the solution here. – ak.leimrey May 05 '22 at 21:28
  • What Heredict says is correct, it helps, but I need to have my object with the same count of records.. If I filter, I will loose those records where is Active is false. – Diego May 06 '22 at 00:18
  • The original array is not affected by `filter`; you don't lose anything. Filter the array into a new array, set the values. Examine the original array; you'll find the values are set to the new ones. – Heretic Monkey May 06 '22 at 12:06

1 Answers1

1

You can use Set to know if UserRole exists in the AccessRole to avoid nested loops. Try the following -

const accessRolesSet = new Set(AccessRoles /*your accessroles string array*/);

for(let r = 0; r < UserRole.length; r++) {
   if(accessRolesSet.has(UserRole[r].id)) {
     UserRole[r].active = true;
   }
}

As suggested by others, the filter will work but it will not set active = true. Hope it helps.

user2216584
  • 5,387
  • 3
  • 22
  • 29
  • Using `Set` does nothing in regards to setting active to true. It just changes the method used to find out if the role exists from `includes` to `has`. – Heretic Monkey May 06 '22 at 12:04
  • OP has asked if he can somehow avoid nested loops. I should have mentioned it. Edited my post now. – user2216584 May 06 '22 at 16:09