当前位置:网站首页>Conversion between flat array and JSON tree
Conversion between flat array and JSON tree
2022-06-27 22:17:00 【You must try, but don't worry】
Flat arrays -> JSON Trees
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 The value returned by the last call callback , Or initial value
prev[item.id] = item
return prev
}, {
})
let result = []
for(let item of flatArr) {
if(!item.parent_id) {
// Class A
result.push(item)
continue
}
// Can get here Definitely not the first level
// Find parent first
if(item.parent_id in map) {
let parent = map[item.parent_id] // Parent
// Classic and concise value taking , No value initialization
;(parent.children || (parent.children = [])).push(item)
}
}
return result
}
JSON Trees -> Flat arrays
const JSONThreeToFlat = tree => {
return tree.reduce((prev, item) => {
const {
children = [], ...rest } = item
return prev.concat( [{
...rest}], JSONThreeToFlat(children) )
}, [])
}
边栏推荐
- [LeetCode]30. Concatenate substrings of all words
- 畅游动态规划之区间DP
- The create database of gbase 8A takes a long time to query and is suspected to be stuck
- Yarn performance tuning of CDH cluster
- Windwos 8.1系统安装vmware tool插件报错的解决方法
- 登录凭证(cookie+session和Token令牌)
- I think I should start writing my own blog.
- 大厂常用软件测试面试题三(附答案)
- 管理系統-ITclub(下)
- Go from introduction to practice -- definition and implementation of behavior (notes)
猜你喜欢

百万年薪独家专访,开发人员不修复bug怎么办?
![\w和[A-Za-z0-9_],\d和[0-9]等价吗?](/img/96/2649c9cf95b06887b57fd8af2d41c2.png)
\w和[A-Za-z0-9_],\d和[0-9]等价吗?
![The problem of minimum modification cost in two-dimensional array [conversion question + shortest path] (dijkstra+01bfs)](/img/e6/4eb2ddf4d9bac5e40bf2e96656d294.png)
The problem of minimum modification cost in two-dimensional array [conversion question + shortest path] (dijkstra+01bfs)
![[LeetCode]动态规划解分割数组I[Red Fox]](/img/b2/df87c3138c28e83a8a58f80b2938b8.png)
[LeetCode]动态规划解分割数组I[Red Fox]

【MySQL】数据库函数通关教程下篇(窗口函数专题)

使用sqlite3语句后出现省略号 ... 的解决方法

Yolov6: the fast and accurate target detection framework is open source

GBase 8a的create database 会被查询耗时很长怀疑卡住的现象分析

Yarn中RMApp、RMAppAttempt、RMContainer和RMNode状态机及其状态转移

年薪50W+的测试大鸟都在用这个:Jmeter 脚本开发之——扩展函数
随机推荐
Yarn中RMApp、RMAppAttempt、RMContainer和RMNode状态机及其状态转移
Go from introduction to actual combat - execute only once (note)
管理系统-ITclub(中)
Codeforces Round #716 (Div. 2)
Codeforces Round #717 (Div. 2)
Interval DP of Changyou dynamic programming
\w和[A-Za-z0-9_],\d和[0-9]等价吗?
Example of using gbase 8A OLAP function group by grouping sets
使用Jmeter进行性能测试的这套步骤,涨薪2次,升职一次
Login credentials (cookie+session and token token)
Contest 2050 and Codeforces Round #718 (Div. 1 + Div. 2)
百万年薪独家专访,开发人员不修复bug怎么办?
Simulink导出FMU模型文件方法
Go 访问GBase 8a 数据库的一个方法
[LeetCode]513. 找树左下角的值
The create database of gbase 8A takes a long time to query and is suspected to be stuck
Stm32f107+lan8720a use stm32subemx to configure network connection +tcp master-slave +udp app
Open source technology exchange - Introduction to Chengying, a one-stop fully automated operation and maintenance manager
Selenium上传文件有多少种方式?不信你有我全!
xpath