当前位置:网站首页>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}是解构
}, []);
},
}
打印结果如下:
边栏推荐
- Arc135 a (time complexity analysis)
- Letter meaning and parameter abbreviation of optical module Daquan
- c语言经典指针和数组笔试题解析
- The data mark is a piece of fat meat, and it is not only China Manfu technology that focuses on this meat
- 光模块字母含义及参数简称大全
- LM小型可编程控制器软件(基于CoDeSys)笔记二十二:错误4268/4052
- Flask
- tutle时钟改进版
- ETCD数据库源码分析——初始化总览
- BUU-Crypto-[GXYCTF2019]CheckIn
猜你喜欢

Introduction to AMBA

ANSYS command

【兴趣阅读】Adversarial Filtering Modeling on Long-term User Behavior Sequences for Click-Through Rate Pre

What are the reasons for the frequent high CPU of ECS?

2022危险化学品经营单位安全管理人员上岗证题库及答案

Graduation design of small programs -- small programs of food and recipes

2022 t elevator repair operation certificate examination question bank and simulation examination

小程序毕业设计---美食、菜谱小程序

拓扑排序和关键路径的图形化显示

Ping port artifact psping
随机推荐
BUU-Real-[PHP]XXE
Thinkphp6.0 middleware with limited access frequency think throttle
LM小型可编程控制器软件(基于CoDeSys)笔记二十二:错误4268/4052
C language simple student management system (including source code)
谷歌 Chrome 浏览器将支持选取文字翻译功能
Flask
Easy change
HMS v1.0 appointment.php editid参数 SQL注入漏洞(CVE-2022-25491)
[MySQL practice of massive data with high concurrency, high performance and high availability -8] - transaction isolation mechanism of InnoDB
Overview of relevant subclasses of beanfactorypostprocessor and beanpostprocessor
[excel] PivotChart
Actual cases and optimization solutions of cloud native architecture
C # character similarity comparison general class
[paper summary] zero shot semantic segmentation
[high concurrency, high performance and high availability of massive data MySQL practice-7] - memory data drop disk
Signification des lettres du module optique et abréviation des paramètres Daquan
Simulink and Arduino serial port communication
Solar insect killing system based on single chip microcomputer
2022年A特种设备相关管理(电梯)考试题模拟考试平台操作
2022年T电梯修理操作证考试题库及模拟考试