I have a problem to filters data inside the array of objects by an array of objects. I already try using filters combine with the includes method but returning an empty array.
let's say I have array an array called listOfPermissions.
listOfPermissions = [
{name:'A',IsChecked:true},
{name:'B',IsChecked:true},
{name:'C',IsChecked:true}
]
Than i want to filter the list with permissionOnRole array
permissionOnRole = [
{name:'C',IsChecked: true}
]
The goals i want to achieve
result = [
{name:'A',IsChecked:true},
{name:'B',IsChecked:true},
]
this is my code
const setUncheckPermissions = () => {
const permissionsOnRole = role.permissions.map(it => ({name: it, isChecked: true}))
const listOfAllPermissions = props.permissions.map((permission) => {return {name: permission['name'], isChecked: true}});
let result = listOfAllPermissions.filter(item => permissionsOnRole.includes(item));
console.log(listOfAllPermissions)
}
please help me to solve this problems Thank you in advance
CodePudding user response:
Just use filter
const listOfPermissions = [
{name:'A',IsChecked:true},
{name:'B',IsChecked:true},
{name:'C',IsChecked:true}
]
const permissionOnRole = [
{name:'C',IsChecked: true}
]
const result = listOfPermissions.filter(item => !!permissionOnRole.find(i => i.name !== item.name))
console.log(result)