当前位置:网站首页>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如何实现数组转树”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注亿速云网站,小编将为大家输出更多高质量的实用文章!
边栏推荐
- Tester's algorithm interview question - find mode
- 如何将自己的代码作品快速存证,已更好的保护自己劳动成果
- QT addition calculator (simple case)
- Advanced template
- In June, the list of winners of "Moli original author program" was announced! Invite you to talk about the domestic database
- [JS] - [sort related] - Notes
- 蓝天NH55系列笔记本内存读写速度奇慢解决过程记录
- 【js】-【动态规划】-笔记
- Fast parsing intranet penetration helps enterprises quickly achieve collaborative office
- Blue sky nh55 series notebook memory reading and writing speed is extremely slow, solution process record
猜你喜欢

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

Galera cluster of MariaDB - dual active and dual active installation settings

CTF競賽題解之stm32逆向入門

How to use fast parsing to make IOT cloud platform

Application of fire fighting system based on 3D GIS platform

【kotlin】第三天
![[JS] - [sort related] - Notes](/img/b7/af467c7a169b73c3c4936072aef8b9.png)
[JS] - [sort related] - Notes

如何有效对直流列头柜进行监测

「运维有小邓」域密码策略强化器

用快解析内网穿透实现零成本自建网站
随机推荐
微软禁用IE浏览器后,打开IE浏览器闪退解决办法
Business implementation - the log is written to the same row of data
雅思考试流程、需要具体注意些什么、怎么复习?
The company needs to be monitored. How do ZABBIX and Prometheus choose? That's the right choice!
高配笔记本使用CAD搬砖时卡死解决记录
js正则表达式之中文验证(转)
go踩坑——no required module provides package : go.mod file not found in current directory or any parent
Hash table, hash function, bloom filter, consistency hash
C语言中sizeof操作符的坑
[crawler] jsonpath for data extraction
快解析内网穿透帮助企业快速实现协同办公
Ffmpeg quick clip
电力运维云平台:开启电力系统“无人值班、少人值守”新模式
Robot reinforcement learning synergies between pushing and grassing with self supervised DRL (2018)
Expand your kubecl function
如果炒股开华泰证券的户,在网上开户安全吗?
Advanced template
Intelligence test to see idioms guess ancient poems wechat applet source code
Illustrated network: what is gateway load balancing protocol GLBP?
Every time I look at the interface documents of my colleagues, I get confused and have a lot of problems...