我只想获取满足条件的那些数据的roleId(resourceId==3 && canView);

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

我有这组数据:

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}
]}
];

在这种情况下,我的输出应该是 [1] 因为 resourceId==3 && canView==true 仅在 roleId 1 和 3 的数据中。我怎样才能以最小的复杂性得到它?

如果 resourceId === 3 && canView,您可以过滤角色,然后映射结果以仅获取角色 ID:

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

它returns只有角色3因为是唯一一个满足条件resourceId === 3 && canView

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