当前位置:网站首页>递归使用和多维数组对象变一维数组对象
递归使用和多维数组对象变一维数组对象
2022-07-03 03:24:00 【MISS_zhang_0110】
递归使用和多维数组对象变一维数组对象
一、递归树形数据
var obj = [
{
id: '1536225102260137986',
name: '六级6',
level: 1,
parentId: '-1',
remark: '',
children: [
{
id: '1539413104865148929',
name: '8',
level: 2,
parentId: '1536225102260137986',
remark: '',
children: [
{
id: '1539425671465500673',
name: '8.1',
level: 3,
parentId: '1539413104865148929',
remark: '',
children: null,
},
{
id: '1539435042027175937',
name: '8.2',
level: 3,
parentId: '1539413104865148929',
remark: '',
children: [
{
id: '1539435619528310786',
name: '8.2.1',
level: 4,
parentId: '1539435042027175937',
remark: '',
children: null,
},
],
},
{
id: '1539446851840868354',
name: '8.3',
level: 3,
parentId: '1539413104865148929',
remark: '',
children: null,
},
],
},
{
id: '1539434602904518658',
name: '8.1',
level: 2,
parentId: '1536225102260137986',
remark: '',
children: null,
},
{
id: '1539446726481510402',
name: '9',
level: 1,
parentId: '1536225102260137986',
remark: '',
children: null,
},
],
},
{
id: '1536236627473526785',
name: '七级7.1',
level: 1,
parentId: '-1',
remark: '',
children: [
{
id: '1536245410367074305',
name: '七级7.1',
level: 1,
parentId: '1536236627473526785',
remark: '',
children: [
{
id: '1539792426864050177',
name: '8.1',
level: 2,
parentId: '1536245410367074305',
remark: '',
children: null,
},
],
},
],
},
{
id: '1539167455968067586',
name: '日期',
level: 1,
parentId: '-1',
remark: '',
children: [
{
id: '1539167630363033602',
name: '今天',
level: 2,
parentId: '1539167455968067586',
remark: '',
children: null,
},
],
},
{
id: '1539794235812835329',
name: 'test',
level: 1,
parentId: '-1',
remark: '',
children: null,
},
{
id: '1539800261748420609',
name: '心情',
level: 1,
parentId: '-1',
remark: '',
children: [
{
id: '1539800324059000833',
name: '好',
level: 2,
parentId: '1539800261748420609',
remark: '',
children: [
{
id: '1539801422505275394',
name: '中',
level: 3,
parentId: '1539800324059000833',
remark: '',
children: null,
},
{
id: '1539805697016950786',
name: '不错',
level: 3,
parentId: '1539800324059000833',
remark: '',
children: null,
},
{
id: '1539806278271987713',
name: '还行',
level: 3,
parentId: '1539800324059000833',
remark: '',
children: null,
},
{
id: '1539808118615142402',
name: '一般般',
level: 3,
parentId: '1539800324059000833',
remark: '',
children: null,
},
{
id: '1539808207265951745',
name: 'OK',
level: 3,
parentId: '1539800324059000833',
remark: '',
children: null,
},
],
},
],
},
]
// 递归
function getJsonTree(data) {
let nowObj = data.map(item => ({
...item,
icon: '图片',
children:
item.children && item.children.length ? getJsonTree(item.children) : null,
}))
return nowObj
}
console.log(obj, getJsonTree(obj))
二、找到树图某个Id下的name (递归)
let id = 1539792426864050177 //name:8.1
let name
function getName(data) {
data.forEach(item => {
if (item.id == id) {
name = item.name
} else {
item.children && item.children.length ? getName(item.children) : null
}
})
return name
}
console.log(getName(obj), 'getName')
三、找到树图某个Id下的name (数组扁平化)
function getSortName(list) {
let res = []
res = list.concat(
...list
.map(item => {
if (item.children instanceof Array && item.children.length > 0) {
return this.getSortName(item.children)
}
return null
})
.filter(o => o instanceof Array && o.length > 0)
)
return res
}
console.log(getSortName(obj), 'getName')
链接: https://wenku.baidu.com/view/7996d7d2f51fb7360b4c2e3f5727a5e9856a2728.html
边栏推荐
- C programming learning notes [edited by Mr. Tan Haoqiang] (Chapter III sequence programming) 05 data input and output
- MongoDB复制集【主从复制】
- 静态网页 和 动态网页的区别 & WEB1.0和WEB2.0的区别 & GET 和 POST 的区别
- softmax的近似之NCE详解
- labelme标记的文件转换为yolov5格式
- MySQL practice 45 lecture [row lock]
- Vs Code configure virtual environment
- UMI route interception (simple and rough)
- [AI practice] Application xgboost Xgbregressor builds air quality prediction model (I)
- Pytoch lightweight visualization tool wandb (local)
猜你喜欢

Limit of one question per day
![45 lectures on MySQL [index]](/img/f6/70be00028908cbd9ed7f2c77687cee.png)
45 lectures on MySQL [index]
![[MySQL] the difference between left join, right join and join](/img/d4/8684cd59cd1bd77e70bd4d7c7074c3.jpg)
[MySQL] the difference between left join, right join and join

Mongodb installation & Deployment

Unity3d RPG implementation (medium)
![Ansible introduction [unfinished (semi-finished products)]](/img/2a/0003daf761ba02d8837c4657fc3f29.png)
Ansible introduction [unfinished (semi-finished products)]

Hi3536C V100R001C02SPC040 交叉编译器安装

The series of hyperbolic function in daily problem

el-tree搜索方法使用
![MySQL Real combat 45 [SQL query and Update Execution Process]](/img/cd/3a635f0c3bb4ac3c8241cb77285cc8.png)
MySQL Real combat 45 [SQL query and Update Execution Process]
随机推荐
PAT乙级常用函数用法总结
Idea set method call ignore case
VS 2019 配置tensorRT生成engine
Limit of one question per day
Advanced redis applications [password protection, data persistence, master-slave synchronization, sentinel mode, transactions] [not completed yet (semi-finished products)]
Convert binary stream to byte array
静态网页 和 动态网页的区别 & WEB1.0和WEB2.0的区别 & GET 和 POST 的区别
VS克隆时显示403错误
About HTTP cache control
Bigvision code
What happens between entering the URL and displaying the page?
Node start server
File rename
Don't use the new Dede collection without the updated Dede plug-in
[combinatorics] Application of exponential generating function (multiple set arrangement problem | different balls in different boxes | derivation of exponential generating function of odd / even sequ
docker安装及启动mysql服务
com.fasterxml.jackson.databind.exc.InvalidFormatException问题
Nanning water leakage detection: warmly congratulate Guangxi Zhongshui on winning the first famous brand in Guangxi
900W+ 数据,从 17s 到 300ms,如何操作
[combinatorics] basic counting principle (addition principle | multiplication principle)