Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

138
Views
I want to get only roleId of those data that satisfies the condition (resourceId==3 && canView);

I have this set of data:

roles = [{roleId:1,resourceRole:[
{canAdd:true,canUpdate:true,canView:true,canDelete:true,resourceId:1},
{canAdd:true,canUpdate:true,canView:true,canDelete:true,resourceId:2},
{canAdd:false,canUpdate:false,canView:false,canDelete:false,resourceId:3}
]},
{roleId:2,resourceRole:[
{canAdd:true,canUpdate:true,canView:true,canDelete:true,resourceId:1},
{canAdd:false,canUpdate:false,canView:false,canDelete:false,resourceId:2},
{canAdd:false,canUpdate:false,canView:false,canDelete:false,resourceId:3}
]},
{roleId:3,resourceRole:[
{canAdd:true,canUpdate:true,canView:true,canDelete:true,resourceId:1},
{canAdd:false,canUpdate:false,canView:false,canDelete:false,resourceId:2},
{canAdd:true,canUpdate:true,canView:true,canDelete:true,resourceId:3}
]}
];

In this case, my output should be [1] because resourceId==3 && canView==true is only in the data with roleId 1 and 3. How can I get this with minimum complexity?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You can filter the roles if resourceId === 3 && canView, then map the results to get only the roleId:

const result = roles
  .filter(({ resourceRole }) =>
    resourceRole.some(({ resourceId, canView }) => resourceId === 3 && canView)
  )
  .map(({ roleId }) => roleId);

It returns only role 3 because is the only one that satisfies the condition resourceId === 3 && canView

Codesandbox: https://codesandbox.io/s/flamboyant-murdock-k70xvp?file=/src/index.js:1545-1549

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!