I have got two arrays of objects . I want to filtering data based on PermissionObj.
This is coming form database. Here is 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"
},
]
}
]
}
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"
}]
},
{
label: "Articles",
},
{
label: "Journals",
content: [{
value: "can_create"
},
{
value: "can_view"
},
]
}
]
I am trying filter data array of object based on PermissionObj array of objects . here is my trying code.
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 .
if PermissionObj value will be matched with data value . then show the data .
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"
},
]
}
]