js算法问题 烧脑 求厉害的小哥看看

let powerData=[

{

id: 'A',

label: '终端管理',

children: [

{label: '设备套餐',router:'devicePackage',id:'devicePackage', parentLabel:'终端管理',parentId:'A',

operation:[

{id:'/deviceManage/getDevicePack',text:"获取套餐列表"},

{id:'/deviceManage/addDevicePack',text:"新增/修改终端套餐"},

]

},

{label: '终端设备',router:'deviceInfo',id:'deviceInfo',parentLabel:'终端管理',parentId:'A',

operation:[

{id:'/deviceManage/getDevicePack',text:"获取套餐列表"},

{id:'/deviceManage/devicePackAddTo',text:"设备套餐追加"},

{id:'/deviceManage/downloadDevice',text:"导出终端设备"},

{id:'/deviceManage/devicePackRecord',text:"查询设备历史套餐"},

{id:'/deviceManage/importDeviceDel',text:"终端设备删除"},

{id:'/deviceManage/importDeviceUpdate',text:"终端设备更新"},

{id:'/deviceManage/importDeviceDetails',text:"终端设备导入"},

{id:'/deviceManage/queryDeviceInfoList',text:"查询设备信息"},

{id:'/deviceManage/bindDeviceSim',text:"设备绑定"}

]

}

]

},{

id: 'B',

label: '网关日志',

children: [

{label: '网关日志',router:'log',id:'log',parentLabel:'网关日志',parentId:'B',

operation:[

{id:'/gatewayLog/findGatewayLog',text:"查询网关日志"}

]

}

]

},{

id: 'C',

label: '系统管理',

children: [

{label: '账号列表',router:'userList',id:'userList',parentLabel:'系统管理',parentId:'C',

operation:[

{id:'/agentManage/addUser',text:"增加/修改用户"},

{id:'/agentManage/queryCurrentUserAllUsers',text:"查询用户的所有用户"},

{id:'/agentManage/queryAgentCustomer',text:"查询下一级代理商"},

]

},{label: '账号权限',router:'accountList',id:'accountList',parentLabel:'系统管理',parentId:'C',

operation:[

{id:'/agentManage/queryCurrentUserAllUsers',text:"查询用户的所有用户"},

{id:'/agentManage/updateUserRights',text:"修改用户权限"},

{id:'/agentManage/queryRightsChckByUserId',text:"查询用户已有权限"},

]

}

]

}

]

let newArr=[

{

buttonUrl: "/deviceManage/getDevicePack",

fPageName: "终端管理",

modular: "A",

pageUrl: "devicePackage",

sPageName: "设备套餐",

},

{

buttonUrl: "/deviceManage/addDevicePack",

fPageName: "终端管理",

modular: "A",

pageUrl: "devicePackage",

sPageName: "设备套餐",

},

{

buttonUrl: "/deviceManage/downloadDevice",

fPageName: "终端管理",

modular: "A",

pageUrl: "deviceInfo",

sPageName: "终端设备",

},

{

buttonUrl: "/gatewayLog/findGatewayLog",

fPageName: "网关日志",

modular: "B",

pageUrl: "log",

sPageName: "网关日志",

}

]

powerData 是总的权限,newArr的数据是后台传给我的,现在想根据 newArr 里的数据,根据newArr里的buttonUrl跟powerData下的operation里的id进行匹配, 把powerData 里的数据过滤成想要的格式,
最终理想的格式 结果是这样

let powerData=[

{

id: 'A',

label: '终端管理',

children: [

{label: '设备套餐',router:'devicePackage',id:'devicePackage', parentLabel:'终端管理',parentId:'A',

operation:[

{id:'/deviceManage/getDevicePack',text:"获取套餐列表"},

{id:'/deviceManage/addDevicePack',text:"新增/修改终端套餐"},

]

},

{label: '终端设备',router:'deviceInfo',id:'deviceInfo',parentLabel:'终端管理',parentId:'A',

operation:[

{id:'/deviceManage/downloadDevice',text:"导出终端设备"},

]

}

]

},

{

id: 'B',

label: '网关日志',

children: [

{label: '网关日志',router:'log',id:'log',parentLabel:'网关日志',parentId:'B',

operation:[

{id:'/gatewayLog/findGatewayLog',text:"查询网关日志"}

]

}

]

}

]

回答

递归呗,然后filter一下。

emm..看上去就两层,也不用递归了。

powerData.forEach((item)=>{

var itemId = item.id;//modular

item.children.forEach((routerItem)=>{

var routerId = routerItem.id;//pageUrl

routerItem.operation = routerItem.operation.filter(operationItem=>{

var operationItemId = operationItem.id

return newArr.find(v=>v.modular == itemId && v.pageUrl == routerId && v.buttonUrl == operationItemId)

})

})

})

image.png


更新时间:2020年9月3日10:42:57
先filter一下,然后再去遍历就可以

var newPowerData \= powerData.filter(item\=>newArr.find(v\=>v.modular \== item.id));

newPowerData.forEach((item)\=>{

var itemId \= item.id;//modular

item.children.forEach((routerItem)\=>{

var routerId \= routerItem.id;//pageUrl

routerItem.operation \= routerItem.operation.filter(operationItem\=>{

var operationItemId \= operationItem.id

return newArr.find(v\=>v.modular \== itemId && v.pageUrl \== routerId && v.buttonUrl \== operationItemId)

})

})

})

newPowerData

image.png

const resNewArr = []

newArr.forEach(item => {

let power = resNewArr.find(r => r.modular === item.modular)

if (!power) {

power = { modular: item.modular, children: [] }

resNewArr.push(power)

}

let child = power.children.find(c => c.pageUrl === item.pageUrl)

if (!child) {

child = { pageUrl: item.pageUrl, operation: [] }

power.children.push(child)

}

child.operation.push({ buttonUrl: item.buttonUrl })

})

const results = []

resNewArr.forEach(item => {

const power = powerData.find(p => p.id === item.modular)

const childrenList = power.children

power.children = []

item.children.forEach(childItem => {

const child = childrenList.find(c => c.id === childItem.pageUrl)

const operationList = child.operation

child.operation = []

childItem.operation.forEach(operateItem => {

const operate = operationList.find(o => o.id === operateItem.buttonUrl)

child.operation.push(operate)

})

power.children.push(child)

})

results.push(power)

})

console.log(results)

想了好几种,我没具体算复杂度,直觉上还是这么算运算步骤最少,就是代码有点长。
我测试过,能符合需求。image

思路就是按照格式和层级先处理后台给的newArr(其实我觉得这一步可以让后台来做),然后按照resNewArrpowerData里逐层去找符合要求的数据,并添加到results里。

以上是 js算法问题 烧脑 求厉害的小哥看看 的全部内容, 来源链接: utcz.com/a/41739.html

回到顶部