当前位置:网站首页>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) )
}, [])
}
边栏推荐
- \w和[A-Za-z0-9_],\d和[0-9]等价吗?
- MySQL greater than less than or equal to symbol representation
- Ellipsis after SQLite3 statement Solutions for
- Beijing University of Posts and Telecommunications - multi-agent deep reinforcement learning for cost and delay sensitive virtual network function placement and routing
- [MySQL practice] query statement demonstration
- [LeetCode]515. Find the maximum value in each tree row
- QT base64 encryption and decryption
- The problem of minimum modification cost in two-dimensional array [conversion question + shortest path] (dijkstra+01bfs)
- [LeetCode]161. Edit distance of 1
- [leetcode] dynamic programming solution partition array ii[arctic fox]
猜你喜欢

Experience sharing of meituan 20K Software Test Engineers

对话乔心昱:用户是魏牌的产品经理,零焦虑定义豪华

美团20k软件测试工程师的经验分享
![[LeetCode]动态规划解分割数组I[Red Fox]](/img/b2/df87c3138c28e83a8a58f80b2938b8.png)
[LeetCode]动态规划解分割数组I[Red Fox]

管理系統-ITclub(下)

Login credentials (cookie+session and token token)
![[leetcode] dynamic programming solution split integer i[silver fox]](/img/18/8dc8159037ec1262444db8899cde0c.png)
[leetcode] dynamic programming solution split integer i[silver fox]

6G显卡显存不足出现CUDA Error:out of memory解决办法

关于davwa的SQL注入时报错:Illegal mix of collations for operation ‘UNION‘原因剖析与验证

Simulink method for exporting FMU model files
随机推荐
Go from introduction to practice -- definition and implementation of behavior (notes)
PCIe knowledge point -008: structure of PCIe switch
MySQL greater than less than or equal to symbol representation
深度学习又有新坑了!悉尼大学提出全新跨模态任务,用文本指导图像进行抠图
[LeetCode]513. 找树左下角的值
[LeetCode]513. Find the value in the lower left corner of the tree
Figure countdownlatch and cyclicbarrier based on AQS queue
The problem of minimum modification cost in two-dimensional array [conversion question + shortest path] (dijkstra+01bfs)
How to do function test well? Are you sure you don't want to know?
matlab查找某一行或者某一列在矩阵中的位置
百万年薪独家专访,开发人员不修复bug怎么办?
軟件測試自動化測試之——接口測試從入門到精通,每天學習一點點
[LeetCode]508. 出現次數最多的子樹元素和
【Redis】零基础十分钟学会Redis
信通院举办“业务与应用安全发展论坛” 天翼云安全能力再获认可
畅游动态规划之区间DP
C语言程序设计详细版 (学习笔记1) 看完不懂,我也没办法。
Software test automation test -- interface test from entry to proficiency, learn a little every day
Management system itclub (medium)
[MySQL practice] query statement demonstration