当前位置:网站首页>递归使用和多维数组对象变一维数组对象
递归使用和多维数组对象变一维数组对象
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
边栏推荐
- 监听对象中值变化及访问
- Converts a timestamp to a time in the specified format
- BigVision代码
- [combinatorics] number of solutions of indefinite equations (number of combinations of multiple sets R | number of non negative integer solutions of indefinite equations | number of integer solutions
- Nasvit: neural architecture search of efficient visual converter with gradient conflict perception hypernetwork training
- 程序员新人上午使用 isXxx 形式定义布尔类型,下午就被劝退?
- PHP constructor with parameters - PHP constructor with a parameter
- MySQL practice 45 lecture [transaction isolation]
- Compare float with 0
- Nce detail of softmax approximation
猜你喜欢

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

Why does thread crash not cause JVM crash

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

MySql实战45讲【索引】

Limit of one question per day

Hi3536c v100r001c02spc040 cross compiler installation

The idea cannot be loaded, and the market solution can be applied (pro test)

umi 路由拦截(简单粗暴)

别再用 System.currentTimeMillis() 统计耗时了,太 Low,StopWatch 好用到爆!

On the adjacency matrix and adjacency table of graph storage
随机推荐
Mysql Mac版下载安装教程
Mongodb master profile
Anhui University | small target tracking: large-scale data sets and baselines
Convert binary stream to byte array
Pytoch configuration
Use three JS make a simple 3D scene
About HTTP cache control
docker安装及启动mysql服务
MongoDB安装 & 部署
idea 加载不了应用市场解决办法(亲测)
labelimg生成的xml文件转换为voc格式
Why does thread crash not cause JVM crash
@Accessors annotation function specifies that the prefix follows the hump naming
VS克隆时显示403错误
VS 2019 配置tensorRT生成engine
Converts a timestamp to a time in the specified format
Section 26 detailed explanation and demonstration of IPSec virtual private network configuration experiment - simulation experiment based on packettracer8.0
el-tree搜索方法使用
为什么线程崩溃不会导致 JVM 崩溃
Using jasmine to monitor constructors - spying on a constructor using Jasmine