当前位置:网站首页>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
边栏推荐
- Explain automatic packing and unpacking?
- EOJ 2020 January race E-number transformation
- Solve proxyerror: CONDA cannot proceed due to an error in your proxy configuration
- Wechat H5 payment on WAP, for non wechat browsers
- Cloud native (36) | introduction and installation of harbor in kubernetes
- JS 一行代码 获取数组最大值与最小值
- 高斯消元求解矩阵的逆(gauss)
- Phpexcel export Emoji symbol error
- copyTo
- 面试突击68:为什么 TCP 需要 3 次握手?
猜你喜欢
Login module use case writing
Wechat applet learning notes 1
图解用户登录验证流程,写得太好了!
2021年山东省中职组“网络空间安全”B模块windows渗透(解析)
Fuzzy PID control of motor speed
一种分布式深度学习编程新范式:Global Tensor
解决npm -v突然失效 无反应
Sqoop【环境搭建 01】CentOS Linux release 7.5 安装配置 sqoop-1.4.7 解决警告并验证(附Sqoop1+Sqoop2最新版安装包+MySQL驱动包资源)
Fiddler packet capturing tool for mobile packet capturing
MQTT X CLI 正式发布:强大易用的 MQTT 5.0 命令行工具
随机推荐
R语言ggpubr包ggsummarystats函数可视化分组箱图(自定义分组颜色)并在X轴标签下方添加分组对应的统计值(样本数N、中位数median、四分位数的间距iqr、统计值的色彩和分组图色匹配
Flutter event distribution
Sqoop【付诸实践 02】Sqoop1最新版 全库导入 + 数据过滤 + 字段类型支持 说明及举例代码(query参数及字段类型强制转换)
高斯消元求解矩阵的逆(gauss)
Uni app learning summary
莫队学习笔记(一)
R language ggpubr package ggsummarystats function visualizes the grouping box diagram (custom grouping color) and adds the statistical values corresponding to the grouping under the x-axis label (samp
服务器环境配置全过程
WARNING: [pool www] server reached pm. max_ children setting (5), consider raising it
R语言ggplot2可视化: 将图例标题(legend title)对齐到ggplot2中图例框的中间(默认左对齐、align legend title to middle of legend)
IIS error prompt after installing Serv-U: hresult:0x80070020
学习笔记之常用数组api 改变原数组和不改变原数组的有哪些?
JS 连等赋值操作
Azkaban【基础知识 01】核心概念+特点+Web界面+架构+Job类型(一篇即可入门Azkaban工作流调度系统)
Leetcode 504. 七进制数
Flutter Event 派发
高斯消元的应用
Qt随手笔记(二)Edit控件及float,QString转化、
服务器、客户端双认证
Application of Gauss elimination