当前位置:网站首页>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
边栏推荐
- 苹果独占鳌头,三星大举复兴,国产手机在高端市场颗粒无收
- JS 一行代码 获取数组最大值与最小值
- [information system project manager] summary of essence of high-level series for the first time
- 分布式网络通信框架:本地服务怎么发布成RPC服务
- EOJ 2020 1月月赛 E数的变换
- IIS网站配置
- Search module use case writing
- Gauss elimination
- Alibaba cloud technology expert haochendong: cloud observability - problem discovery and positioning practice
- Server and client dual authentication (2)
猜你喜欢

PMM (percona monitoring and management) installation record

The problem of accessing certsrv after configuring ADCs

Solve proxyerror: CONDA cannot proceed due to an error in your proxy configuration

Keeping alive to realize MySQL automatic failover

服务发现原理分析与源码解读

Node 内存溢出及V8垃圾回收机制

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

Alibaba cloud technology expert haochendong: cloud observability - problem discovery and positioning practice

Fiddler download and installation

2019 ICPC Asia Yinchuan Regional(水题题解)
随机推荐
Solve NPM -v sudden failure and no response
Wechat applet development
2021年山东省中职组“网络空间安全”B模块windows渗透(解析)
Wechat H5 payment on WAP, for non wechat browsers
Alibaba cloud technology expert haochendong: cloud observability - problem discovery and positioning practice
PHP one-time request lifecycle
asp. Net using redis cache
服务器、客户端双认证
Flutter Event 派发
B站这个视频我是跪着看完的
Show default image when wechat applet image cannot be displayed
Solve proxyerror: CONDA cannot proceed due to an error in your proxy configuration
Matlab Simulink realizes fuzzy PID control of time-delay temperature control system of central air conditioning
官方颁发的SSL证书与自签名证书结合实现网站双向认证
R语言ggpubr包ggsummarystats函数可视化分组箱图(自定义分组颜色)并在X轴标签下方添加分组对应的统计值(样本数N、中位数median、四分位数的间距iqr、统计值的色彩和分组图色匹配
JS 一行代码 获取数组最大值与最小值
WARNING: [pool www] server reached pm. max_ children setting (5), consider raising it
反射机制的原理是什么?
Keeping alive to realize MySQL automatic failover
高斯消元求解矩阵的逆(gauss)