当前位置:网站首页>JS扁平化数形结构的数组
JS扁平化数形结构的数组
2022-07-04 05:29:00 【_处女座程序员的日常】
mounted() {
//已知JSON树状形结构的数据data
let jsonData = [
{
id: 1, title: '标题1', parent_id: 0, },
{
id: 2, title: '标题2', parent_id: 0,
children: [
{
id: 3, title: '标题2-1', parent_id: 2,
children: [
{
id: 4, title: '标题3-1', parent_id: 3,
children: [
{
id: 5, title: '标题4-1', parent_id: 4 }
]
}
]
},
{
id: 6, title: '标题2-2', parent_id: 2 }
]
}
];
console.table(this.flat(jsonData));
}
//mounted中:
flat(data) {
return data.reduce((pre, cur) => {
// console.log(cur);
//此处将对象的children属性和值都解构在空数组中,将对象的其他属性和值都解构在i里面。
const {
children = [], ...i } = cur; // 注意 ...i 只能写在解构赋值的末尾,否则报错
// console.log(i);
// // console.log(children);
return pre.concat([{
...i }], this.flat(children)) //利用递归回调,数组合并,注意此处 {...i}是解构
}, []);
},
}
打印结果如下:
边栏推荐
- 总线的基本概念
- ping端口神器psping
- Thread pool: use thread pool to optimize query speed
- How much computing power does transformer have
- Leetcode 184 Employees with the highest wages in the Department (July 3, 2022)
- 1480. Dynamic sum of one-dimensional array
- Excel comparator
- Graduation design of small programs -- small programs of food and recipes
- C语言简易学生管理系统(含源码)
- Simulated small root pile
猜你喜欢

BUU-Pwn-test_ your_ nc

Introduction To AMBA 简单理解

ping端口神器psping

KMP match string

Principle and practice of common defects in RSA encryption application
![[technology development -25]: integration technology of radio and television network, Internet, telecommunication network and power grid](/img/87/e0469e280365ed0261e2b551ebd888.png)
[technology development -25]: integration technology of radio and television network, Internet, telecommunication network and power grid

2022年R2移动式压力容器充装复训题库及答案

input显示当前选择的图片

Accidentally deleted the data file of Clickhouse, can it be restored?

Analysis of classical pointer and array written test questions in C language
随机推荐
Graduation design of small programs -- small programs of food and recipes
[matlab] general function of communication signal modulation - generation of narrow-band Gaussian white noise
Talk about the SQL server version of DTM sub transaction barrier function
JS string splicing
Appearance of LabVIEW error dialog box
flink1.13 sql基础语法(一)DDL、DML
flink1.13 sql基础语法(二)join操作
一键过滤选择百度网盘文件
Yyds dry goods inventory TCP & UDP
Use of hutool Pinyin tool
Void convolution, deformable convolution, deformable ROI pooling
Ping port artifact psping
补某视频网站的js,进行视频解密
2022 a special equipment related management (elevator) examination questions simulation examination platform operation
BUU-Crypto-[GXYCTF2019]CheckIn
ping端口神器psping
Excel 比较日器
Flink1.13 basic SQL syntax (II) join operation
2022 t elevator repair operation certificate examination question bank and simulation examination
IP时代来临,电竞酒店如何借好游戏的“东风”?