Home > Software design >  Array and array of objects
Array and array of objects

Time:12-19

I have an array and an array of objects

'neededPermissions: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]',

'permissions:[{permissionType: 1, teamId: 1},{permissionType: 4, teamId: 1},{permissionType: 7, teamId: 1},{permissionType: 8, teamId: 1},{permissionType: 10, teamId: 1}]'

// route with neededPermissions array must include with the permissions in current user, then the user will have access to the route // permissions.includes(neededPermissions)=true => user have access to the route

I know how it works for two arrays, but How to work with array and array of objects?

CodePudding user response:

You may need to do custom filteration based on array of objects,

let resultPermissions = permissions.filter((x)=>{ 
 return neededPermissions.includes(x.permissionType)
})

CodePudding user response:

const permissions = [{permissionType: 1, teamId: 1},{permissionType: 4, teamId: 1},{permissionType: 7, teamId: 1},{permissionType: 8, teamId: 1},{permissionType: 10, teamId: 1}];
const neededPermissions = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11];
const validPermissions = permissions.filter((item) => {
    return neededPermissions.includes(item.permissionType);
});

// returned matched permissions
console.log(validPermissions);
  • Related