当前位置:网站首页>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}是解构
}, []);
},
}
打印结果如下:
边栏推荐
- JS string splicing enhancement
- VB. Net simple processing pictures, black and white (class library - 7)
- Excel 比较日器
- BeanFactoryPostProcessor 与 BeanPostProcessor 相关子类概述
- LM small programmable controller software (based on CoDeSys) note 22: error 4268/4052
- ANSYS command
- Principle and practice of common defects in RSA encryption application
- Analysis of classical pointer and array written test questions in C language
- [matlab] communication signal modulation general function interpolation function
- Input displays the currently selected picture
猜你喜欢

LM small programmable controller software (based on CoDeSys) note 22: error 4268/4052

数据标注是一块肥肉,盯上这块肉的不止中国丨曼孚科技
![[QT] create mycombobox click event](/img/5a/ed17567a71f6737891fc7a8273df0a.png)
[QT] create mycombobox click event

Build an Internet of things infrared temperature measuring punch in machine with esp32 / rush to work after the Spring Festival? Baa, no matter how hard you work, you must take your temperature first

ANSYS command

Actual cases and optimization solutions of cloud native architecture

Evolution of system architecture: differences and connections between SOA and microservice architecture

Flutter ‘/usr/lib/libswiftCore. dylib‘ (no such file)

2022年T电梯修理操作证考试题库及模拟考试

【雕爷学编程】Arduino动手做(105)---压电陶瓷振动模块
随机推荐
XII Golang others
How to clone objects
企业级日志分析系统ELK(如果事与愿违那一定另有安排)
Flink1.13 SQL basic syntax (I) DDL, DML
2022 question bank and answers for safety management personnel of hazardous chemical business units
Online shrimp music will be closed in January next year. Netizens call No
[technology development -25]: integration technology of radio and television network, Internet, telecommunication network and power grid
Analysis of classical pointer and array written test questions in C language
谷歌 Chrome 浏览器将支持选取文字翻译功能
Leetcode 184 Employees with the highest wages in the Department (July 3, 2022)
空洞卷积、可变形卷积、可变形ROI Pooling
[QT] create mycombobox click event
补某视频网站的js,进行视频解密
Arc135 C (the proof is not very clear)
Automated testing selenium foundation -- webdriverapi
1480. 一维数组的动态和
Actual cases and optimization solutions of cloud native architecture
Simulated small root pile
力扣(LeetCode)184. 部门工资最高的员工(2022.07.03)
【QT】制作MyComboBox点击事件