解决方案
方法一
export function getTreeMenuRoutes(arr
) {
const menuData
= []
arr
.forEach(item
=> {
if (item
.pid
=== 0) {
menuData
.push(item
)
}
})
menuData
.forEach(item
=> {
item
.children
= []
arr
.forEach(item1
=> {
if (item
.id
=== item1
.pid
) {
item
.children
.push(item1
)
}
})
})
return menuData
}
方法二
export function setTreeData(arr
) {
if (JSON.stringify(arr
) === '{}') {
return
}
arr
.forEach(function(item
) {
delete item
.children
})
const map
= {}
arr
.forEach(i
=> {
map
[i
.id
] = i
})
const treeData
= []
arr
.forEach(child
=> {
const mapItem
= map
[child
.pid
]
if (mapItem
) {
(mapItem
.children
|| (mapItem
.children
= [])).push(child
)
} else {
treeData
.push(child
)
}
})
return treeData
}
级联选择器
export function getCascaderData(arr
) {
var newArr
= []
const map
= new Map()
arr
.forEach(item
=> {
if (map
.has(item
.category
)) {
map
.get(item
.category
).children
.push({ value
: item
.id
,
label
: item
.appname
, id
: item
.id
})
} else {
map
.set(item
.category
,
{
label
: item
.category
,
value
: item
.appname
,
id
: item
.id
,
children
: [{ value
: item
.id
, id
: item
.id
,
label
: item
.appname
}]
})
}
})
newArr
.push(...map
.values())
return newArr
}
转载请注明原文地址: https://lol.8miu.com/read-2995.html