I have an array named A as following:
{
name: 'activityRelatedManager',
meta: {
title: 'Activity Related Manager',
permissions: []
},
children: [
{
name: 'activityPublish',
meta: { title: 'Publish Activity', permissions:[] }
},
{
name: 'activityManager',
meta: { title: 'Manage Activity', permissions:[] }
},
{
name: 'activityTypeManager',
meta: { title: ' Manage Activity Type', permissions:[] }
},
{
name: 'activityLocationManager',
meta: { title: ' Manage Activity Location', permissions:[] }
}
]
},
I also have an array named B like this:
[
{
"id": 1,
"name": "activityRelatedManager",
"permissions": [
"activity:publish"
]
},
{
"id": 2,
"name": "activityManager",
"permissions": [
"activity:index",
"activity:show"
]
},
{
"id": 3,
"name": "activityTypeManager",
"permissions": []
}
]
What i want to do is to find the duplicated values based on 'name' key in B, and then assign the permissions value to A. The result should be something like this:
{
name: 'activityRelatedManager',
meta: {
title: 'Activity Related Manager',
permissions: ["activity:publish"]
},
children: [
{
name: 'activityManager',
meta: { title: 'Manage Activity', permissions:["activity:index", "activity:show"] }
},
{
name: 'activityTypeManager',
meta: { title: ' Manage Activity Type', permissions:[] }
}
]
},
I think the assignment is easy, but how to find the duplication?
A.map(ele => {
if (!ele.name || (!ele.meta && !ele.meta.permissions)) return
for (let i = 0; i < B.length; i++) {
const element = B[i]
if (ele.name === element.name) {
ele.meta.permissions = element.permissions
}
}
if (ele.children) {
makePermissionRouters(B, A.children)
}
})
return clientAsyncRoutes
I'd appreciate it if someone can help me solve this question.
activityLocationManager. But if so, what do you want to happen if the parent node of an included node would not be included? That is, what would happen ifactivityRelatedManagerhad no match in B?activityLocationManagerhad children that had a match in B? You don't include it now. Would you include it then?activityLocationManagerwill not include the children that will have a match in B.