当前位置:网站首页>递归使用和多维数组对象变一维数组对象
递归使用和多维数组对象变一维数组对象
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
边栏推荐
- Basic information of Promethus (I)
- The series of hyperbolic function in daily problem
- PHP constructor with parameters - PHP constructor with a parameter
- The XML file generated by labelimg is converted to VOC format
- 使用InputFilter限制EditText时踩坑及解决方案
- float与0比较
- Tidal characteristics of the Bohai Sea and the Yellow Sea
- Converts a timestamp to a time in the specified format
- 基于QT的tensorRT加速的yolov5
- MongoDB基本操作【增、删、改、查】
猜你喜欢
随机推荐
监听对象中值变化及访问
为什么线程崩溃不会导致 JVM 崩溃
MongoDB簡介
File rename
Nanning water leakage detection: warmly congratulate Guangxi Zhongshui on winning the first famous brand in Guangxi
Nasvit: neural architecture search of efficient visual converter with gradient conflict perception hypernetwork training
Basic information of Promethus (I)
Pytoch lightweight visualization tool wandb (local)
Limit of one question per day
Pytorch multi card distributed training distributeddataparallel usage
Why does thread crash not cause JVM crash
VS 2019安装及配置opencv
Change and access of median value of listening object
[mathematical logic] propositions and connectives (propositions | propositional symbolization | truth connectives | no | conjunction | disjunction | non truth connectives | implication | equivalence)
The calculation of stripe, kernel and padding in CNN
静态网页 和 动态网页的区别 & WEB1.0和WEB2.0的区别 & GET 和 POST 的区别
[Chongqing Guangdong education] cultural and natural heritage reference materials of China University of Geosciences (Wuhan)
redis高级应用【密码防护、数据持久化、主从同步、哨兵模式、事务】【暂未完成(半成品)】
labelme标记的文件转换为yolov5格式
C programming learning notes [edited by Mr. Tan Haoqiang] (Chapter III sequence programming) 03 operators and expressions