当前位置:网站首页>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
}
边栏推荐
- 协同工具满足70%-90%的工作需求,成为企业香饽饽
- 神经网络的三种训练方法,神经网络训练全过程
- It is obvious that a unique index is added, why does it still generate duplicate data?
- uni 小程序腾讯地图polygon背景透明度
- Rust learning: 6.1_Slices of composite types
- 11111
- Using the color picker
- Rust learning: 6.2_ Tuples of composite types
- AFNetworking概述和4.0的实践
- Day36 LeetCode
猜你喜欢
随机推荐
Introduction to C integer data storage
张驰课堂:老板会武术,谁也挡不住!六西格玛培训的魅力
js reduce
速卖通卖家如何抓住产品搜索权重
什么是长轮询
神经网络样本太少怎么办,神经网络训练样本太少
占位占位1
30条实用MySQL优化法则
Rust学习:6.4_复合类型之枚举
ATH10 sensor reads temperature and humidity
Compilation failure:找不到符号
CV-人脸识别-2018:ArcFace
菜鸟、小白在autojs和冰狐智能辅助之间如何选择?
JS reduce
Based on STC8G2K64S4 single-chip microcomputer to display analog photosensitive analog value through OLED screen
全连接神经网络结构图,神经网络示意图怎么画
Go-Excelize API source code reading (11) - GetActiveSheetIndex()
2022-08-01 Advanced Network Engineering (24) STP Advanced Knowledge
【FAQ】【Push Kit】推送服务,回执配置一直报错、回执过期修改、怎么删除配置的回执
Power function Exponential function Logarithmic function