当前位置:网站首页>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) )
}, [])
}
边栏推荐
- matlab查找某一行或者某一列在矩阵中的位置
- Common problems encountered by burp Suite
- Management system itclub (Part 1)
- [LeetCode]161. 相隔为 1 的编辑距离
- 石子合并问题分析
- [LeetCode]186. 翻转字符串里的单词 II
- 北京邮电大学|用于成本和延迟敏感的虚拟网络功能放置和路由的多智能体深度强化学习
- [MySQL] database function clearance Tutorial Part 2 (window function topic)
- . Net learning notes (V) -- lambda, LINQ, anonymous class (VaR), extension method
- Software test automation test -- interface test from entry to proficiency, learn a little every day
猜你喜欢

01 golang environment construction

Educational Codeforces Round 108 (Rated for Div. 2)

年薪50W+的测试大鸟都在用这个:Jmeter 脚本开发之——扩展函数

Use Fiddler to simulate weak network test (2g/3g)

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

Common problems encountered by burp Suite
![\W and [a-za-z0-9_], \Are D and [0-9] equivalent?](/img/96/2649c9cf95b06887b57fd8af2d41c2.png)
\W and [a-za-z0-9_], \Are D and [0-9] equivalent?

AQS SOS AQS with me

Codeforces Round #716 (Div. 2)

Remote invocation of microservices
随机推荐
美团20k软件测试工程师的经验分享
[LeetCode]572. 另一棵树的子树
BAT测试专家对web测试和APP测试的总结
Gbase 8A OLAP analysis function cume_ Example of dist
关于davwa的SQL注入时报错:Illegal mix of collations for operation ‘UNION‘原因剖析与验证
STM32CubeIDE1.9.0\STM32CubeMX 6.5 F429IGT6加LAN8720A,配置ETH+LWIP
使用Jmeter进行性能测试的这套步骤,涨薪2次,升职一次
MYSQL和MongoDB的分析
扁平数组和JSON树的转换
[LeetCode]508. 出现次数最多的子树元素和
How to design an elegant caching function
北京邮电大学|用于成本和延迟敏感的虚拟网络功能放置和路由的多智能体深度强化学习
Yolov6: the fast and accurate target detection framework is open source
01 golang environment construction
Bean paste green protects your eyes
Experience sharing of meituan 20K Software Test Engineers
[LeetCode]515. Find the maximum value in each tree row
[MySQL practice] query statement demonstration
哈希表-数组之和
crontab定时任务常用命令