当前位置:网站首页>js如何实现数组转树
js如何实现数组转树
2022-07-04 23:49:00 【亿速云】
js如何实现数组转树
本篇内容介绍了“js如何实现数组转树”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!
原生 封装工具函数 getTree
1.1 定义
-映射对象 map
数组 treeList=[]
1.2 遍历后端返回的数组 list 为 每个数组对象item 添加 children 属性 值为空数组

1.3 为映射对象 map 添加属性 并赋值key:id值 value:item

1.4 遍历数组对象list 当item.pid为空时 为一级目录 将该一级目录数组对象 添加到treeList中

1.5 通过 item.pid获取到 id 不为空时 获取数组对象 pid为01

1.6 获取 映射对象 map 获取到 属性名01 将该数组对象item 添加 到children中 并返回到treeList中
代码:

结构图:

“js如何实现数组转树”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注亿速云网站,小编将为大家输出更多高质量的实用文章!
边栏推荐
猜你喜欢

如何避免电弧产生?—— AAFD故障电弧探测器为您解决
![[IELTS reading] Wang Xiwei reading P3 (heading)](/img/19/40564f2afc18fe3e34f218b7b44681.png)
[IELTS reading] Wang Xiwei reading P3 (heading)

PMP证书续证流程

香港珠宝大亨,22亿“抄底”佐丹奴

多回路仪表在基站“转改直”方面的应用

Design of emergency lighting evacuation indication system for urban rail transit station

"Xiaodeng" domain password policy enhancer in operation and maintenance

取得PMP证书需要多长时间?

端口映射和端口转发区别是什么

The difference between cout/cerr/clog
随机推荐
[crawler] XPath for data extraction
Observable time series data downsampling practice in Prometheus
端口映射和端口转发区别是什么
Excel shortcut keys - always add
go踩坑——no required module provides package : go.mod file not found in current directory or any parent
The input of uniapp is invalid except for numbers
Significance of acrel EMS integrated energy efficiency platform in campus construction
The initial trial is the cross device model upgrade version of Ruijie switch (taking rg-s2952g-e as an example)
打新债开户注册安全吗?有没有风险的?靠谱吗?
Fast analysis -- easy to use intranet security software
Chinese verification of JS regular expressions (turn)
Cross domain request
Jar batch management gadget
Basic points of the game setup of the points mall
The caching feature of docker image and dockerfile
S32 design studio for arm 2.2 quick start
ECCV 2022 | Tencent Youtu proposed disco: the effect of saving small models in self supervised learning
Robot reinforcement learning synergies between pushing and grassing with self supervised DRL (2018)
Financial markets, asset management and investment funds
Illustrated network: what is gateway load balancing protocol GLBP?