强化并优化一下代码 const list = await store.dispatch('user/getMenuRole') const menuList = [] let hiddenMenuList = null for (let i = 0; i < list.length; i++) { const item = list[i] if (item.hidden) { for (let index = 0; index < hiddenList.length; index++) { const vmItme = hiddenList[index] const childrenData = [] vmItme.children.forEach((v, vIndex) => { item.children.forEach(s => { if (v.name === s.meta.menuName) { s.meta.childrenBtnList.length > 0 && ( v.meta.childrenBtnList = s.meta.childrenBtnList ) childrenData.push(v) const routerList = vmItme routerList.children = childrenData hiddenMenuList = routerList } }) }) } } else { menuList.push(item) } }
时间: 2024-01-04 18:03:28 浏览: 119
下面是进一步强化并优化后的代码:
```
const list = await store.dispatch('user/getMenuRole')
const menuList = []
const hiddenMenuList = []
for (const item of list) {
if (item.hidden) {
const childrenData = hiddenList.flatMap(vmItme => {
const children = vmItme.children.filter(v => item.children.some(s => v.name === s.meta.menuName))
children.forEach(v => {
const s = item.children.find(s => v.name === s.meta.menuName)
if (s.meta.childrenBtnList.length > 0) {
v.meta.childrenBtnList = s.meta.childrenBtnList
}
})
vmItme.children = children
return vmItme
})
hiddenMenuList.push(...childrenData)
} else {
menuList.push(item)
}
}
```
这里使用了 `Array.prototype.flatMap()` 和 `Array.prototype.some()` 方法代替了嵌套的循环逻辑,使代码更加简洁易读。同时,使用扩展运算符 `...` 将数组展开,避免了多余的赋值操作。
阅读全文