当前位置:网站首页>js中树与数组的相互转化(树的子节点若为空隐藏children字段)
js中树与数组的相互转化(树的子节点若为空隐藏children字段)
2022-07-26 09:21:00 【周小盗】
写博客是为了记笔记
前言
在使用elmentUI
或者antUI
等中的 cascader
级联选择器时 ,需要树级结构,有时就会需要进行树与数组的相互转换,这里的树与数组的转化将子children为空时省去了,避免出现选择时空白页的情况(如图一)
一、树转数组
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
}
二、数组转树
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
}
}
})
}
三、使用
1.data中定义的数据(树)options
options: [
{
value: 'zhinan',
label: '指南',
parentId:'0',
id: "aa",
children: [
{
value: 'shejiyuanze',
label: '设计原则',
parentId:'aa',
id: "aa01",
children: [
{
value: 'yizhi',
label: '一致',
parentId:'aa01',
id: "aa0101",
},
{
value: 'fankui',
label: '反馈',
parentId:'aa01',
id: "aa0102",
},
]
},
]
},
{
value: 'zujian',
label: '组件',
parentId:'0',
id: "bb",
children: [
{
value: 'basic',
label: 'Basic',
parentId:'bb',
id: "bb01",
children: [
{
value: 'layout',
label: 'Layout 布局',
parentId:'bb01',
id: "bb0101",
},
]
},
{
value: 'form',
label: 'Form',
parentId:'bb',
id: "bb02",
children: []
},
]
},
],
2.使用树转数组(注意转成数组时默认删除children
属性)
let list = this.treeToArray(this.options)
console.log("list",list)
2.使用数组转树
let tree = this.listToTree(list,list[0].parentId)
console.log("tree",tree)
注意这里当children
为[]
时,默认不挂载children
属性
边栏推荐
- Qt | 关于如何使用事件过滤器 eventFilter
- 暑假第四周
- Zipkin安装和使用
- What is the difference between NFT and digital collections?
- Polynomial open root
- Innovus is stuck, prompting x error:
- js在控制台输出菱形
- 对象型的集合按某个属性的值进行去重
- Li Mu D2L (IV) -- softmax regression
- 2022 tea artist (intermediate) special operation certificate examination question bank simulated examination platform operation
猜你喜欢
【Mysql】redo log,undo log 和binlog详解(四)
Advanced mathematics | Takeshi's "classic series" daily question train of thought and summary of error prone points
Innovus is stuck, prompting x error:
【Mysql】Mysql锁详解(三)
Unity topdown character movement control
Voice chat app source code - Nath live broadcast system source code
Under a directory of ext3 file system, subfolders cannot be created, but files can be created
[MySQL] detailed explanation of MySQL lock (III)
CF1481C Fence Painting
Two tips for pycharm to open multiple projects
随机推荐
arc-gis基础操作3
760. String length
What are CSDN spaces represented by
Mysql事务
MySQL strengthen knowledge points
PHP 之 Apple生成和验证令牌
[use of final keyword]
垂直搜索
【Mysql】Mysql锁详解(三)
【Mysql】redo log,undo log 和binlog详解(四)
[MySQL] detailed explanation of MySQL lock (III)
760. 字符串长度
Study notes of canal
HBuilderX 运行微信开发者工具 “Fail to open IDE“报错解决
Local cache
【Mysql】一条SQL语句是怎么执行的(二)
Processing of inconsistent week values obtained by PHP and MySQL
NTT(快速数论变换)多项式求逆 一千五百字解析
Windows通过命令备份数据库到本地
Use of off heap memory