当前位置:网站首页>js-----数组转换成树形结构
js-----数组转换成树形结构
2022-08-10 08:22:00 【cc&】
/**
* Created by PanJiaChen on 16/11/18.
*/
/**
* 公用方法
* @param {*} list
*/
/**
* 转换树形数据
* @param {*} list 需要转换的部门数据
* @returns
*/
export function listToTreeData(list) {
const treeList = []
const map = {}
list.forEach((item) => {
map[item.id] = item
})
list.forEach((item) => {
if (item.pid === -1) return
const parent = map[item.pid]
if (parent) {
if (!parent.children) {
parent.children = []
}
parent.children.push(item)
} else {
treeList.push(item)
}
})
return treeList
}边栏推荐
猜你喜欢
随机推荐
C# 获取PCI等设备的插槽位置信息
预测股票涨跌看什么指标,如何预测明天股票走势
CV-人脸识别-2018:ArcFace
CV+Deep Learning - network architecture Pytorch recurrence series - classification (3: MobileNet, ShuffleNet)
js reduce
Class Notes (7) (1) - #647. Find the root and the child (root)
自动化测试框架Pytest(三)——自定义allure测试报告
初使jest 单元测试
硬件工程师90天学习资料及笔记汇总20220730
11111
raid5的写性能,是不的比raid10快一些?
【搜索引擎】Solr:提高批量索引的性能
快速输入当前日期与时间
Rust学习:6.5_复合类型之数组
详解构建mock服务最方便的神器——Moco
A File Online Query Display and Download Function Realized by Delphi
自动化测试框架Pytest(二)——前后置处理
Day37 LeetCode
LaTeX出现错误代码Command \algorithmic already defined
Day36 LeetCode








