我有一个问题,通过对象数组来过滤对象数组中的数据。我已经尝试将过滤器与include方法结合使用,但返回一个空数组。
假设我有一个数组一个名为listOfPermissions的数组
listOfPermissions = [
{name:'A',IsChecked:true},
{name:'B',IsChecked:true},
{name:'C',IsChecked:true}
]
然后我想用permissionOnRole数组过滤列表
permissionOnRole = [
{name:'C',IsChecked: true}
]
我想达到的目标
result = [
{name:'A',IsChecked:true},
{name:'B',IsChecked:true},
]
这是我的代码
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)
}
请帮我解决这个问题提前感谢
就用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)