问题
I have got two arrays of objects. I want to filter data based on permissionObj.
This is coming from database. Here are arrays of sub-arrays in the permissionObj.
let permissionObj = [{
"OA deal": [{
label: "can view",
value: "can_view"
}, {
label: "can create",
value: "can_create"
},
{
label: "can edit",
value: "can_edit"
}, {
label: "can delete",
value: "can_delete"
}
]
},
{
"Deposit": [{
label: "can view",
value: "can_view"
}, {
label: "can create",
value: "can_create"
},
{
label: "can edit",
value: "can_edit"
}, {
label: "can delete",
value: "can_delete"
}
]
},
{
"Journals": [{
label: "can view",
value: "can_view"
}, {
label: "can create",
value: "can_create"
}, {
label: "can edit",
value: "can_edit"
}, {
label: "can delete",
value: "can_delete"
}]
},
{
"Deposit": [{
label: "can view",
value: "can_view"
}, {
label: "can create",
value: "can_create"
}, {
label: "can edit",
value: "can_edit"
}, {
label: "can delete",
value: "can_delete"
}]
},
{
"OA deals request": []
},
{
"Deposit Approval": []
},
{
"Dashboard": [{
label: "can view",
value: "can_view"
}, {
label: "can create",
value: "can_create"
}, {
label: "can edit",
value: "can_edit"
}, {
label: "can delete",
value: "can_delete"
}]
},
]
this is static data. I want to compare this data based on permission.
console.log(permission)
const PubSidebar = [{
label: "Dashboard",
key: "Dashboard",
value: "can_view"
}, {
label: "Settings",
key: "Settings",
value: "can_view"
},
{
label: "OA deal activities",
key:"OA Deal" || "Deposit"|| "Corrections" ,
content: [{
label: "Add oadeal",
key: "OA deal",
value: "can_create"
},
{
label: "edit oadeal ",
key: "OA deal",
value: "can_edit"
},
{
label: "view oadeal ",
key: "OA deal",
value: "can_view"
},
{
label: "Deposit for view",
key: "Deposit",
value: "can_view"
},
{
label: "Deposit for edit",
key: "Deposit",
value: "can_edit"
},
{
key: "Deposit",
value: "can_edit"
},
{
label: "edit Corrections",
key: "Corrections",
value: "can_edit"
},
{
label: "Add Corrections",
key: "Corrections",
value: "can_create"
},
{
label: "view Corrections",
key: "Corrections",
value: "can_view"
}
]
},
{ key:"Journals"
label: "Journal for publishers",
content: [{
label: "Add Journal",
key: "Journals",
value: "can_create"
},
{
label: "Journals List",
key: "Journals",
value: "can_view"
},
]
},
];
I am trying to filter the PubSidebar array of the object based on permissionObj array of objects. here is my trying code.
let permission =
permissionObj.filter(
x => Object.values(x)[0].length !== 0
);
const result =
PubSidebar &&
PubSidebar.reduce((accumulator, currentvalue) => {
let filterObj = permission && permission.find(f => f[currentvalue.key]);
// console.log("filterObj", filterObj);
if (filterObj) {
if (currentvalue.value) {
accumulator.push(currentvalue);
}
if (currentvalue.content) {
console.log(currentvalue);
currentvalue.content =
currentvalue &&
currentvalue.content &&
currentvalue.content.filter(
f =>
filterObj[currentvalue.key] &&
filterObj[currentvalue.key].some(
s =>
s.value &&
s.value.toLowerCase() &&
s.value === f.value.toLowerCase()
)
);
accumulator.push(currentvalue);
}
}
return accumulator;
}, []);
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 corrections. In my permission objects, There is no Corrections.
I want to filtering pubsidebar based on key and value of permissionObj objects. It will be this format value
// My Accepted Output would be :
const output = [{
label: "Dashboard",
key: "Dashboard",
value: "can_view"
},
{ key:"OA Deal" || "Deposit",
label: "OA deal activities",
content: [{
label: "Add oadeal",
key: "OA deal",
value: "can_create"
},
{
label: "edit oadeal ",
key: "OA deal",
value: "can_edit"
},
{
label: "view oadeal ",
key: "OA deal",
value: "can_view"
},
{
label: "Deposit for view",
key: "Deposit",
value: "can_view"
},
{
label: "Deposit for edit",
key: "Deposit",
value: "can_edit"
},
{
key: "Deposit",
value: "can_edit"
},
]
},
{ key:"Journals"
label: "Journal for publishers",
content: [{
label: "Add Journal",
key: "Journals",
value: "can_create"
},
{
label: "Journals List",
key: "Journals",
value: "can_view"
},
]
},
];
来源:https://stackoverflow.com/questions/60930341/how-to-filter-array-of-objects-in-js