如何在JavaScript中的子数组数组和子数组数组之间进行过滤? [英] How to filtering between array of sub arrays and array of sub arrays in javascript?
问题描述
我有两个对象数组。我想根据 PermissionObj
过滤数据。
I have got two arrays of objects. I want to filter data based on PermissionObj
.
这是来自数据库。这是 permissionObj
中的子数组的数组。
This is coming from database. Here are arrays of sub-arrays in the 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
比较此数据。
this is static data. I want to compare this data based on 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过滤对象的数据数组
对象数组。这是我尝试的代码。
I am trying to filter the data array of the object based on PermissionObj
array of objects. here is my trying code.
const permKeys = PermissionObj.permission.flatMap(item => Object.keys(item));
const filteredData = data.filter(({ label }) => permKeys.includes(label));
console.log(filteredData);
这是我的问题,我一直面临着我不想得到 can_edit
, can_delete
(如果与日记中的权限对象不匹配)。在我的权限对象中,日记中没有 can_edit
和 can_delete
。
Here is my problem, I have been faced is that I don't want to get can_edit
, can_delete
if it doesn't match with permission objects in journals. In my permission objects, There is no can_edit
and can_delete
in journals.
我接受的输出将是以下格式:
My accepted Output would be this format :
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"
}
]
}
]
推荐答案
可以使用 reduce
方法并应用逻辑来决定应推送哪些数据:
It is possible to use reduce
method and apply logic to decide what data should be pushed:
const result = data.reduce((a, c) => {
let filterObj = PermissionObj.permission.find(f => f[c.label]);
if (filterObj) {
if (c.value) {
a.push(c);
}
if (c.content) {
c.content = c.content.filter(f => filterObj[c.label]
.some(s => s.value.toLowerCase() == f.value.toLowerCase()));
a.push(c);
}
}
return a;
},[])
示例:
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"
}
]
}
]
};
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"
}
]
}
];
const result = data.reduce((a, c) => {
let filterObj = PermissionObj.permission.find(f => f[c.label]);
if (filterObj) {
if (c.value) {
a.push(c);
}
if (c.content) {
c.content = c.content.filter(f => filterObj[c.label].some(s => s.value.toLowerCase() == f.value.toLowerCase()));
a.push(c);
}
}
return a;
},[])
console.log(result);
这篇关于如何在JavaScript中的子数组数组和子数组数组之间进行过滤?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!