当前位置:网站首页>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}是解构
}, []);
},
}
打印结果如下:
边栏推荐
- left_and_right_net可解释性设计
- Leetcode 184 Employees with the highest wages in the Department (July 3, 2022)
- BUU-Crypto-[GUET-CTF2019]BabyRSA
- Enterprise level log analysis system elk (if things backfire, there must be other arrangements)
- Void convolution, deformable convolution, deformable ROI pooling
- [matlab] general function of communication signal modulation - generation of narrow-band Gaussian white noise
- 2022 R2 mobile pressure vessel filling retraining question bank and answers
- Penetration tool - sqlmap
- ansys命令
- Simulink and Arduino serial port communication
猜你喜欢
Supplement the JS of a video website to decrypt the video
Upper computer software development - log information is stored in the database based on log4net
2022g2 power station boiler stoker special operation certificate examination question bank and answers
[wechat applet] template and configuration (wxml, wxss, global and page configuration, network data request)
Two sides of the evening: tell me about the bloom filter and cuckoo filter? Application scenario? I'm confused..
724. 寻找数组的中心下标
一键过滤选择百度网盘文件
空洞卷积、可变形卷积、可变形ROI Pooling
ANSYS command
Flutter calls Gaode map app to realize location search, route planning and reverse geocoding
随机推荐
2022G2电站锅炉司炉特种作业证考试题库及答案
小程序毕业设计---美食、菜谱小程序
[high concurrency, high performance and high availability of massive data MySQL practice-7] - memory data drop disk
left_and_right_net正常版本
Electronic components mall and data manual download website summary
c语言经典指针和数组笔试题解析
Thread pool: use thread pool to optimize query speed
Etcd database source code analysis - initialization overview
[matlab] general function of communication signal modulation - generation of narrow-band Gaussian white noise
Trie number dictionary tree
Risc-v-qemu-virt in FreeRTOS_ Lock mechanism analysis of GCC
Write a complete answer applet (including single choice questions, judgment questions and multiple topics) (III) single choice questions, judgment questions, and the first question display
fastjson
Arc135 a (time complexity analysis)
1.1 history of Statistics
谷歌 Chrome 浏览器将支持选取文字翻译功能
Supplement the JS of a video website to decrypt the video
1480. Dynamic sum of one-dimensional array
【雕爷学编程】Arduino动手做(105)---压电陶瓷振动模块
With the advent of the IP era, how can E-sports hotels take advantage of the "east wind" of games?