当前位置:网站首页>Transform between tree and array in JS (hide the children field if the child node of the tree is empty)
Transform between tree and array in JS (hide the children field if the child node of the tree is empty)
2022-07-26 09:53:00 【Zhou Xiaotiao】
Blogging is for taking notes
Preface
In the use of elmentUI perhaps antUI Isochronous cascader When cascading selectors , Tree structure required , Sometimes it is necessary to convert trees and arrays , The transformation of tree and array here will be sub children There is no need for empty time , Avoid choosing white pages in time and space ( As shown in Fig. 1 )
One 、 Tree to array
treeToArray(list, newArr = []) {
list.forEach(item => {
const {
children} = item
if (children) {
delete item.children
if (children.length) {
newArr.push(item)
return this.treeToArray(children, newArr)
}
}
newArr.push(item)
})
return newArr
}
Two 、 Array to tree
listToTree(list, parentId = null) {
return list.filter(item => item.parentId === parentId).map(item => {
let children= this.listToTree(list, item.id)
if (children.length > 0) {
return {
...item,
children
}
} else {
return {
...item
}
}
})
}
3、 ... and 、 Use
1.data Data defined in ( Trees )options
options: [
{
value: 'zhinan',
label: ' guide ',
parentId:'0',
id: "aa",
children: [
{
value: 'shejiyuanze',
label: ' Design principles ',
parentId:'aa',
id: "aa01",
children: [
{
value: 'yizhi',
label: ' Agreement ',
parentId:'aa01',
id: "aa0101",
},
{
value: 'fankui',
label: ' feedback ',
parentId:'aa01',
id: "aa0102",
},
]
},
]
},
{
value: 'zujian',
label: ' Components ',
parentId:'0',
id: "bb",
children: [
{
value: 'basic',
label: 'Basic',
parentId:'bb',
id: "bb01",
children: [
{
value: 'layout',
label: 'Layout Layout ',
parentId:'bb01',
id: "bb0101",
},
]
},
{
value: 'form',
label: 'Form',
parentId:'bb',
id: "bb02",
children: []
},
]
},
],
2. Use tree to array ( Note that it is deleted by default when converting to an array children attribute )
let list = this.treeToArray(this.options)
console.log("list",list)

2. Use array to turn the tree
let tree = this.listToTree(list,list[0].parentId)
console.log("tree",tree)

Notice here when children by [] when , Not mounted by default children attribute
边栏推荐
- 分布式网络通信框架:本地服务怎么发布成RPC服务
- 2022年中科磐云——服务器内部信息获取 解析flag
- 网络流学习笔记
- Redis sentinel mode setup under Windows
- 2021 windows penetration of "Cyberspace Security" B module of Shandong secondary vocational group (analysis)
- Leetcode 504. 七进制数
- The whole process of server environment configuration
- QT handy notes (VI) -- update interface, screenshot, file dialog box
- 莫队学习笔记(一)
- Registration module use case writing
猜你喜欢

Use of tabbarcontroller

spolicy请求案例

Logical architecture of MySQL

2022 zhongkepan cloud - server internal information acquisition and analysis flag
![Sqoop [environment setup 01] CentOS Linux release 7.5 installation configuration sqoop-1.4.7 resolve warnings and verify (attach sqoop 1 + sqoop 2 Latest installation package +mysql driver package res](/img/8e/265af6b20f79b21c3eadcd70cfbdf7.png)
Sqoop [environment setup 01] CentOS Linux release 7.5 installation configuration sqoop-1.4.7 resolve warnings and verify (attach sqoop 1 + sqoop 2 Latest installation package +mysql driver package res

Mqtt x cli officially released: powerful and easy-to-use mqtt 5.0 command line tool

Installation and use of cocoapods

阿里云技术专家郝晨栋:云上可观测能力——问题的发现与定位实践

解决npm -v突然失效 无反应
![[datawhale] [machine learning] Diabetes genetic risk detection challenge](/img/98/7981af7948feb73168e5200b3dfac9.png)
[datawhale] [machine learning] Diabetes genetic risk detection challenge
随机推荐
CSV data file settings of JMeter configuration components
EOJ 2020 1月月赛 E数的变换
高斯消元的应用
论文笔记(SESSION-BASED RECOMMENDATIONS WITHRECURRENT NEURAL NETWORKS)
Wechat applet learning notes 1
高斯消元求解矩阵的逆(gauss)
Sqoop【付诸实践 02】Sqoop1最新版 全库导入 + 数据过滤 + 字段类型支持 说明及举例代码(query参数及字段类型强制转换)
copyTo
【Datawhale】【机器学习】糖尿病遗传风险检测挑战赛
Qt随手笔记(二)Edit控件及float,QString转化、
IIS website configuration
图解用户登录验证流程,写得太好了!
Matlab Simulink realizes fuzzy PID control of time-delay temperature control system of central air conditioning
Mo team learning summary (II)
M-ary number STR to n-ary number
Alibaba cloud technology expert haochendong: cloud observability - problem discovery and positioning practice
挡不住了,纯国产PC已就位,美国的软硬件体系垄断正式被破
antd TreeSelect获取父节点的值
QT handy notes (VI) -- update interface, screenshot, file dialog box
编写一个在bash / shell 和 PowerShell中均可运行的脚本