1

I have an array similar to this

 export const PRIVILEGES: privileges[] =   [
  {
    Role: ['admin','trainer','trainee','designer'],
    Home: true,
    UserManagment:  true,
    DefineTraining:true,
   AssignTraining:  true ,
    AssignedTraining:  true,
    PerformanceAnalysis:  true
  },
  {
    Role: ['admin','trainee'],
    Home: true,
    UserManagment:  true,
    DefineTraining:false,
   AssignTraining:  false ,
    AssignedTraining:  true,
    PerformanceAnalysis: true
  }]

and I have a Role defined as

Role=['admin','trainee']

"I want to get the object of PRIVILEGES where Role matches PRIVILEGES.Role"

I have tried

PRIVILEGES.find(x => x.Role == this.Role);

but this won't work because Role is an array and even though both x.Role and this.Role are same it returns a negative result.

FizzaTahir
  • 57
  • 4
  • 1
    Little bit rude this is really no duplicated question but w/e. What you can do is use `let keys = Object.keys(PRIVILEGES); for (let PRIVILEGE in keys) { let object = PRIVILEGE.find(x => x.Role == this.Role); }` hope this helps. But check Maksyms responce it's better. – Swoox Sep 12 '18 at 13:38
  • 2
    try `PRIVILEGES.filter(x => x.Role.indexOf(this.Role) > -1);` find will return only first match, filter will return an array of all matches – Maksym Shevchenko Sep 12 '18 at 13:39
  • Above comments assume that `this.Role` is a string, but it is an array. The question is therefore asking how to compare two arrays as equal. So it is a duplicate question and the linked question has many useful answers. If you think this is not the case and can explain why, I'd be happy to reopen the question. – jcalz Sep 12 '18 at 13:58
  • if `this.Role` is an array, then you need to write your own compare function. It will be formatted badly, can't post an answer here. `compareArrays(arr1: Array, arr2: Array): boolean { if (arr1.length !== arr2.length) { return false; } let match: boolean = true; for (let i = 0; i < arr1.length) { if (arr1[i] === arr2[i]) { return } match = false; } return match; }` – Maksym Shevchenko Sep 12 '18 at 14:07

0 Answers0