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
}
将一个扁平数组转换成树状结构
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。