function toChildrenStruct(pidArray, expandLevel) {
// 将pid结构的树型数据转换成children结构
const childrenArray = []
const expandedItems = []
const itemMap = {}
if (!pidArray) {
return childrenArray
} else {
for (const item of pidArray) {
item.key = item.id
item.value = item.id
item.label = item.name
itemMap[item.id] = item
}
for (const item of pidArray) {
const parent = itemMap[item.parentId]
if (typeof parent === 'undefined') {
// pid不存在,是顶级元素
childrenArray.push(item)
} else {
if (typeof parent.children === 'undefined') {
parent.children = []
if (expandLevel === -1) expandedItems.push(parent.id)
}
parent.children.push(item)
}
}
}
return childrenArray
}
网友评论