当前位置:网站首页>扁平数组和JSON树的转换
扁平数组和JSON树的转换
2022-06-27 19:45:00 【你一定要努力,但千万别着急】
扁平数组 -> JSON树
let flatArr = [
{
id: 1, title: "title1", parent_id: 0 },
{
id: 2, title: "title2", parent_id: 0 },
{
id: 3, title: "title2-1", parent_id: 2 },
{
id: 4, title: "title3-1", parent_id: 3 },
{
id: 5, title: "title4-1", parent_id: 4 },
{
id: 6, title: "title3-2", parent_id: 3 }
]
const flatArrToJSONTree = arr => {
let map = flatArr.reduce((prev, item) => {
// prev 上一次调用回调返回的值,或初始值
prev[item.id] = item
return prev
}, {
})
let result = []
for(let item of flatArr) {
if(!item.parent_id) {
// 一级
result.push(item)
continue
}
// 能到这里 肯定不是第一级
// 先找到父级
if(item.parent_id in map) {
let parent = map[item.parent_id] // 父级
// 经典简洁的有值取值,无值初始化
;(parent.children || (parent.children = [])).push(item)
}
}
return result
}
JSON树 -> 扁平数组
const JSONThreeToFlat = tree => {
return tree.reduce((prev, item) => {
const {
children = [], ...rest } = item
return prev.concat( [{
...rest}], JSONThreeToFlat(children) )
}, [])
}
边栏推荐
- \W and [a-za-z0-9_], \Are D and [0-9] equivalent?
- Gao fushuai in the unit testing industry, pytest framework, hands-on teaching, will do this in the future test reports~
- Fill in the blank of rich text test
- Set code exercise
- GBase 8a OLAP分析函数 cume_dist的使用样例
- 管理系统-ITclub(下)
- xpath
- BAT测试专家对web测试和APP测试的总结
- [leetcode] dynamic programming solution partition array i[red fox]
- [LeetCode]513. 找树左下角的值
猜你喜欢

Open source technology exchange - Introduction to Chengying, a one-stop fully automated operation and maintenance manager

Stm32f107+lan8720a use stm32subemx to configure network connection +tcp master-slave +udp app

Fill in the blank of rich text test
![[leetcode] dynamic programming solution partition array ii[arctic fox]](/img/a1/4644206db3e14c81f9f64e4da046bf.png)
[leetcode] dynamic programming solution partition array ii[arctic fox]

不外泄的测试用例设计秘籍--模块测试
![[MySQL] database function clearance Tutorial Part 2 (window function topic)](/img/03/2b37e63d0d482d5020b7421ac974cb.jpg)
[MySQL] database function clearance Tutorial Part 2 (window function topic)

Professor of Tsinghua University: software testing has gone into a misunderstanding - "code is necessary"

Special training of guessing game

Process control task

STM32F107+LAN8720A使用STM32cubeMX配置网络连接+tcp主从机+UDP app
随机推荐
GBase 8a OLAP函数group by grouping sets的使用样例
qt 大文件生成md5校验码
Matlab finds the position of a row or column in the matrix
软件缺陷管理——测试人员必会
[leetcode] dynamic programming solution partition array ii[arctic fox]
[LeetCode]508. 出現次數最多的子樹元素和
IO stream code
C language programming detailed version (learning note 1) I can't understand it after reading, and I can't help it.
Professor of Tsinghua University: software testing has gone into a misunderstanding - "code is necessary"
[LeetCode]动态规划解拆分整数I[Silver Fox]
我想我要开始写我自己的博客了。
Codeforces Round #717 (Div. 2)
QT large file generation MD5 check code
BAT测试专家对web测试和APP测试的总结
Interval DP of Changyou dynamic programming
Installing Oracle11g under Linux
Go from introduction to actual combat - task cancellation (note)
QT base64 encryption and decryption
Set code exercise
[sword offer ii] sword finger offer II 029 Sorted circular linked list