13

I have this data:

roles = [
{roleId: "69801", role: "ADMIN"}
{roleId: "69806", role: "SUPER_ADMIN"}
{roleId: "69805", role: "RB"}
{roleId: "69804", role: "PILOTE"}
{roleId: "69808", role: "VENDEUR"}
{roleId: "69807", role: "SUPER_RB"}
]

i have to filter my table to check if there is an object containing a specifie value of role .

My function should look like this :

checkRoleExistence(role){

// if role exists on one of the objects return true
// else returne false
}

to use it i would do s.th like this :

let ifExists = this.checkRoleExistence("PILOTE") ;

I would like to use the "filter" function of Ecmascript .

Suggestions ?

3
  • My function should look like this - it's empty? I don't think it should look like that at all - I think you need to put some code in there Commented Jul 24, 2018 at 12:18
  • @trichetriche but there's no need for filter and it's in french :( Commented Jul 24, 2018 at 12:29
  • You're the one asking for filter, I personally would have used some. And you can change the language ! Commented Jul 24, 2018 at 12:30

3 Answers 3

35

You can use some method and destructuring.

let roles = [ {roleId: "69801", role: "ADMIN"}, {roleId: "69806", role: "SUPER_ADMIN"}, {roleId: "69805", role: "RB"}, {roleId: "69804", role: "PILOTE"}, {roleId: "69808", role: "VENDEUR"}, {roleId: "69807", role: "SUPER_RB"} ]

const checkRoleExistence = roleParam => roles.some( ({role}) => role == roleParam)

console.log(checkRoleExistence("ADMIN"));
console.log(checkRoleExistence("RA"));
console.log(checkRoleExistence("RB"));

Sign up to request clarification or add additional context in comments.

2 Comments

(upvoted) Or an ES6 one-liner : const checkRoleExistence = roleParam => roles.some( ({role}) => role == roleParam)
@SwapNeil, arrow functions are not supported in IE, you should use babel or just write a callback method for some.
7

a little addition to all the answers given here. You can use find() to get value which matches your requirement.

const index = this.roles.findIndex(role=> role.name === 'ADMIN'); if (index >-1) { const value= this.roles[index].roleId); }

this will give you roleId , where it matches your query

2 Comments

The problem with that is that if .find() does not find the element, it returns null, and your script will crash with Cannot read property 'roleId' of null.
That's what the if statement is for if (index >-1) then it found the element at the specified index. So it would never be null.
2

I got this solution for you: check this out

export class RoleComponent implements OnInit {
  roles: Role[] = [];
  isRoleExist:boolean = false;
  constructor() { }

  ngOnInit() {
    const data = this.getRoles();
    this.roles = JSON.parse(data);

    this.isRoleExist = this.checkRoleExistence('PILOTE');
    console.log(this.isRoleExist);
  }

  checkRoleExistence(roleLabel: string):boolean {
    return this.roles.some(r => r.roleLabel === roleLabel);
  }

  getRoles() {
    return `[
    {"roleId": "69801", "roleLabel": "ADMIN"},
    {"roleId": "69806", "roleLabel": "SUPER_ADMIN"},
    {"roleId": "69805", "roleLabel": "RB"},
    {"roleId": "69804", "roleLabel": "PILOTE"},
    {"roleId": "69808", "roleLabel": "VENDEUR"},
    {"roleId": "69807", "roleLabel": "SUPER_RB"}
    ]`;
  }
}

export class Role {
  roleId: number;
  roleLabel: string;
}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.