Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
116
rated 0 times [  117] [ 1]  / answers: 1 / hits: 46524  / 6 Years ago, tue, july 24, 2018, 12:00:00

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 ?


More From » arrays

 Answers
14

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




[#53900] Friday, July 20, 2018, 6 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
briarc

Total Points: 402
Total Questions: 85
Total Answers: 114

Location: El Salvador
Member since Sun, Sep 12, 2021
3 Years ago
;