我有两个对象数组。我想根据 PermissionObj 过滤数据。
这是来自数据库。这是permissionObj 中的子数组数组。
const PermissionObj = {
permission: [{
"books": [{
"label": "Can View",
"value": "can_view"
}]
},
{
"Journals": [{
"label": "Can View",
"value": "can_view"
},
{
"label": "Can Create",
"value": "can_create"
},
]
},
{
"deal": [{
"label": "Can update",
"value": "can_update"
},
{
"label": "Can delete",
"value": "can_delete"
},
]
}
]
}
这是静态数据。我想根据 PermissionObj 比较这些数据。
const data = [{
label: "books",
value: "can_view"
},
{
label: "deal",
content: [{
value: "can_update"
}, {
value: "can_delete"
},{value:"can_view"}]
},
{
label: "Articles",
},
{
label: "Journals",
content: [{
value: "can_create"
},
{
value: "can_view"
},
{
value: "can_delete"
},
{
value: "can_edit"
},
]
}
]
我正在尝试基于PermissionObj对象数组过滤对象数据数组。这是我的尝试代码。
let permission = PermissionObj.permission
permission.filter(item=>{
// I am finding data label by using permission label.
let dataItem = data.find(index=>item[index.label])
console.log(dataItem)
})
// the problem is that I want to filtering based on value .
// if both of value is true , then show the data .
如果 PermissionObj 值将与数据值匹配。然后显示数据。
我接受的输出将是这种格式:
const data = [{
label: "books",
value: "can_view"
},
{
label: "deal",
content: [{
value: "can_update"
}, {
value: "can_delete"
}]
},
{
label: "Journals",
content: [{
value: "can_create"
},
{
value: "can_view"
},
]
}
]