当前位置:网站首页>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]515. Find the maximum value in each tree row
- [LeetCode]508. 出現次數最多的子樹元素和
- [sword offer ii] sword finger offer II 029 Sorted circular linked list
- Contest 2050 and Codeforces Round #718 (Div. 1 + Div. 2)
- 深度学习又有新坑了!悉尼大学提出全新跨模态任务,用文本指导图像进行抠图
- Summary of gbase 8A database user password security related parameters
- Experience sharing of meituan 20K Software Test Engineers
- 真香,自从用了Charles,Fiddler已经被我彻底卸载了
- C language programming detailed version (learning note 1) I can't understand it after reading, and I can't help it.
- 哈希表-数组之和
猜你喜欢

Système de gestion - itclub (II)
![[leetcode] dynamic programming solution partition array ii[arctic fox]](/img/a1/4644206db3e14c81f9f64e4da046bf.png)
[leetcode] dynamic programming solution partition array ii[arctic fox]

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

读写分离-Mysql的主从复制

Go from introduction to practice - error mechanism (note)

不外泄的测试用例设计秘籍--模块测试
![[LeetCode]动态规划解拆分整数I[Silver Fox]](/img/18/8dc8159037ec1262444db8899cde0c.png)
[LeetCode]动态规划解拆分整数I[Silver Fox]

真香,自从用了Charles,Fiddler已经被我彻底卸载了

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

信通院举办“业务与应用安全发展论坛” 天翼云安全能力再获认可
随机推荐
对话乔心昱:用户是魏牌的产品经理,零焦虑定义豪华
Introduction to ARCS Model
Codeforces Round #722 (Div. 2)
STM32CubeIDE1.9.0\STM32CubeMX 6.5 F429IGT6加LAN8720A,配置ETH+LWIP
Example of using gbase 8A OLAP function group by grouping sets
Simulink method for exporting FMU model files
BAT测试专家对web测试和APP测试的总结
QT base64 encryption and decryption
Hash table - sum of arrays
Slow bear market, bit Store provides stable stacking products to help you cross the bull and bear
Read write separation master-slave replication of MySQL
[LeetCode]161. 相隔为 1 的编辑距离
畅游动态规划之区间DP
Codeforces Round #719 (Div. 3)
Yolov6: the fast and accurate target detection framework is open source
Golang uses regularity to match substring functions
记一次List对象遍历及float类型判断大小
[Sword Offer II]剑指 Offer II 029. 排序的循环链表
软件测试自动化测试之——接口测试从入门到精通,每天学习一点点
Go from introduction to actual combat - only any task is required to complete (notes)