当前位置:网站首页>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}是解构
}, []);
},
}
打印结果如下:
边栏推荐
- Flink1.13 basic SQL syntax (II) join operation
- [paper summary] zero shot semantic segmentation
- flink1.13 sql基础语法(一)DDL、DML
- Notepad++--显示相关的配置
- Redis realizes ranking function
- Leetcode 184 Employees with the highest wages in the Department (July 3, 2022)
- With the advent of the IP era, how can E-sports hotels take advantage of the "east wind" of games?
- Topological sorting and graphical display of critical path
- Overview of relevant subclasses of beanfactorypostprocessor and beanpostprocessor
- (4) Canal multi instance use
猜你喜欢

Halcon图片标定,使得后续图片处理过后变成与模板图片一样

Simulink与Arduino串口通信
![[high concurrency, high performance and high availability of massive data MySQL practice-7] - memory data drop disk](/img/b9/cf4db4f8a5d2ef3fb344258f0e30f5.jpg)
[high concurrency, high performance and high availability of massive data MySQL practice-7] - memory data drop disk

Introduction to AMBA

C # character similarity comparison general class

VB. Net GIF (making and disassembling - optimizing code, class library - 5)

2022g2 power station boiler stoker special operation certificate examination question bank and answers
![[paper summary] zero shot semantic segmentation](/img/78/ee64118d86a7e43ec4d1cb97191fbe.jpg)
[paper summary] zero shot semantic segmentation

SQL injection - injection based on MSSQL (SQL Server)

LM小型可编程控制器软件(基于CoDeSys)笔记二十一:错误3703
随机推荐
What are the reasons for the frequent high CPU of ECS?
补某视频网站的js,进行视频解密
724. Find the central subscript of the array
2022 question bank and answers for safety management personnel of hazardous chemical business units
Simulink与Arduino串口通信
Thinkphp6.0 middleware with limited access frequency think throttle
Flask
PostgreSQL has officially surpassed mysql. Is this guy too strong!
Void convolution, deformable convolution, deformable ROI pooling
光模块字母含义及参数简称大全
企业级日志分析系统ELK(如果事与愿违那一定另有安排)
VB. Net calls ffmpeg to simply process video (class Library-6)
transformer坑了多少算力
Risc-v-qemu-virt in FreeRTOS_ Lock mechanism analysis of GCC
input显示当前选择的图片
Flask
Accidentally deleted the data file of Clickhouse, can it be restored?
[Excel] 数据透视图
Canoe panel learning video
Character types of C language