当前位置:网站首页>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如何实现数组转树”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注亿速云网站,小编将为大家输出更多高质量的实用文章!
边栏推荐
- Solution record of jamming when using CAD to move bricks in high configuration notebook
- Business implementation - the log is written to the same row of data
- CTF競賽題解之stm32逆向入門
- 端口映射和端口转发区别是什么
- Hash table, hash function, bloom filter, consistency hash
- In June, the list of winners of "Moli original author program" was announced! Invite you to talk about the domestic database
- How to save your code works quickly to better protect your labor achievements
- Pytoch --- use pytoch to realize linknet for semantic segmentation
- Go pit - no required module provides Package: go. Mod file not found in current directory or any parent
- 同事的接口文档我每次看着就头大,毛病多多。。。
猜你喜欢
![[IELTS reading] Wang Xiwei reads P4 (matching2 paragraph information matching question [difficult])](/img/83/63296108b47eda37c19b9ff9deb5ec.png)
[IELTS reading] Wang Xiwei reads P4 (matching2 paragraph information matching question [difficult])

公司要上监控,Zabbix 和 Prometheus 怎么选?这么选准没错!

The difference between debug and release

取得PMP證書需要多長時間?

How long does it take to obtain a PMP certificate?

Every time I look at the interface documents of my colleagues, I get confused and have a lot of problems...

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

French scholars: the explicability of counter attack under optimal transmission theory

In June, the list of winners of "Moli original author program" was announced! Invite you to talk about the domestic database

用快解析内网穿透实现零成本自建网站
随机推荐
MariaDB's Galera cluster application scenario -- multi master and multi active databases
Stm32 Reverse Introduction to CTF Competition Interpretation
Phpcms paid reading function Alipay payment
The caching feature of docker image and dockerfile
圖解網絡:什麼是網關負載均衡協議GLBP?
Galera cluster of MariaDB - dual active and dual active installation settings
壁仞科技研究院前沿技术文章精选
【北京大学】Tensorflow2.0-1-开篇
Face recognition 5- insight face padding code practice notes
Advanced template
PMP证书续证流程
ICML 2022 || 3DLinker: 用于分子链接设计的E(3)等变变分自编码器
【js】-【排序-相关】-笔记
Intelligence test to see idioms guess ancient poems wechat applet source code
【雅思阅读】王希伟阅读P4(matching1)
股票账户佣金怎么调低,炒股佣金怎么调低网上开户安全吗
How to reduce the stock account Commission and stock speculation commission? Is it safe to open an online account
【雅思阅读】王希伟阅读P3(Heading)
雅思考试流程、需要具体注意些什么、怎么复习?
公司要上监控,Zabbix 和 Prometheus 怎么选?这么选准没错!