当前位置:网站首页>递归使用和多维数组对象变一维数组对象
递归使用和多维数组对象变一维数组对象
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
边栏推荐
- New programmers use the isXXX form to define Boolean types in the morning, and are discouraged in the afternoon?
- Stop using system Currenttimemillis() takes too long to count. It's too low. Stopwatch is easy to use!
- 【AI实战】应用xgboost.XGBRegressor搭建空气质量预测模型(一)
- com. fasterxml. jackson. databind. Exc.invalidformatexception problem
- [Chongqing Guangdong education] cultural and natural heritage reference materials of China University of Geosciences (Wuhan)
- Pat class B "1104 forever" DFS optimization idea
- Use three JS make a simple 3D scene
- 为什么线程崩溃不会导致 JVM 崩溃
- VS 2019安装及配置opencv
- Introduction à mongodb
猜你喜欢

VS 2019安装及配置opencv

Nanning water leakage detection: warmly congratulate Guangxi Zhongshui on winning the first famous brand in Guangxi

idea 加载不了应用市场解决办法(亲测)

Elsevier latex 提交文章 pdftex.def Error: File `thumbnails/cas-email.jpeg‘ not found: using draf

Idea set method call ignore case

Introduction to mongodb
![MySQL practice 45 [global lock and table lock]](/img/23/fd58c185ae49ed6c04f1a696f10ff4.png)
MySQL practice 45 [global lock and table lock]

ffmpeg下载安装教程及介绍

Summary of electromagnetic spectrum

Latest version of NPM: the "NPM" item cannot be recognized as the name of a cmdlet, function, script file, or runnable program. Please check
随机推荐
docker安装及启动mysql服务
MySql实战45讲【行锁】
45 lectures on MySQL [index]
MongoDB主配置文件
Ansible简介【暂未完成(半成品)】
Avec trois. JS fait une scène 3D simple
Tidal characteristics of the Bohai Sea and the Yellow Sea
FileZilla Client下載安裝
softmax的近似之NCE详解
用Three.js做一個簡單的3D場景
Unity3d RPG implementation (medium)
Basic operations of mongodb [add, delete, modify, query]
Pytoch configuration
VS 2019 配置tensorRT生成engine
Small guide for rapid formation of manipulator (VIII): kinematic modeling (standard DH method)
Pat class B common function Usage Summary
node 开启服务器
PHP generates PDF tcpdf
Vs 2019 configure tensorrt to generate engine
[leetcode question brushing day 34] 540 Unique element in array, 384 Disrupt array, 202 Happy number, 149 Maximum number of points on a line