如何在reactjs中按对象数组过滤对象数组

hs1rzwqc  于 2022-12-12  发布在  React
关注(0)|答案(1)|浏览(194)

我在用对象数组过滤对象数组中的数据时遇到了问题。我已经尝试使用过滤器和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)

}

请帮我解决这个问题提前感谢您

jdzmm42g

jdzmm42g1#

只需使用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)

相关问题