1

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?

2 Answers 2

0

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

let resultPermissions = permissions.filter((x)=>{ 
 return neededPermissions.includes(x.permissionType)
})
Sign up to request clarification or add additional context in comments.

2 Comments

for the above example, resultPermission should return false, right? because permissions array does not have all the permissions in neededPermissions array.
you can apply negation in front of includes method result
0
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);

2 Comments

So, can I check whether the validPermissions===neededPermissions, then router should go to certain page. Will this work? if (validPermissions === to.meta.neededPermissions) { router.push({path:'/somePage'}) }
You need to make both data same. Array can't be compared like this. They share address rather than value in assignment. Another way can be: let mappedValidPermissions = validPermissions.map((item) => { return item.permissionType; }); Now can be compared as below: JSON.stringify(mappedValidPermissions) === JSON.stringify(neededPermissions);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.