当前位置:网站首页>Use of recursion: 1. Convert the tiled array to a tree 2. Convert the tree to a tiled array
Use of recursion: 1. Convert the tiled array to a tree 2. Convert the tree to a tiled array
2022-07-28 01:56:00 【No lazy duck】
Tile array
const flatData= [
{
id: '1', parentId: 'root', name:'1'},
{
id: '1-1', parentId: '1', name:'1-1'},
{
id: '1-2', parentId: '1', name:'1-2'},
{
id: '1-1-1', parentId: '1-1', name:'1-1-1'},
{
id: '1-1-2', parentId: '1-1', name:'1-1-2'},
{
id: '1-2-1', parentId: '1-2', name:'1-2-1'}
];
Tree array targetData
const targetData=[
{
id: '1',
parentId: 'root',
name:'1',
children:[
{
id: '1-1',
parentId: '1',
name:'1-1',
children:[
{
id: '1-1-1',
parentId: '1-1',
name:'1-1-1'
},
{
id: '1-1-2',
parentId: '1-1',
name:'1-1-2'
},
]
},
{
id: '1-2',
parentId: '1',
name:'1-2',
children:[
{
id: '1-2-1',
parentId: '1-2',
name:'1-2-1'
},
]
},
]
},
]
Specific code implementation :
1. Turn a tiled array into a tree
Key points : Father's id It's a child's parentId
function getTreeData(data,id){
let arr = []
data.forEach((item,index) => {
let children = getTreeData(data,item.id)
if(children.length){
item.children = children
}
arr.push(item)
});
return arr
}
const treeData= getTreeData(flatData, 'root');
console.log('tree', targetArr);
2. Convert a tree to a tiled array
let flatData= []
function getFlatData(data){
data.forEach((item,index)=>{
if(item.children&&item.children.length){
getTreeFlatData(item.children)
delete item.children
}
flatData.push(item)
})
}
getFlatData(treeData)
console.log(flatData)
边栏推荐
- The storage cost is reduced by 80%. How does the cost management of youzan data middle office do?
- What is method and methodology: understand the underlying logic of self-improvement
- Interview question 01.06. string compression
- 负载均衡SLB
- 基于 Flink CDC 实现海量数据的实时同步和转换
- ros2 launch文件常用模块
- Gbase 8C general file access function
- Summary: Prometheus storage
- Leetcode's 83rd biweekly match
- C # learning notes ----- C # connect to MySQL database
猜你喜欢

N32L43x Flash读\写\擦除操作总结

26. Abstraction and template ideas

Tencent cloud hiflow scene connector

HRD 1. a simple and reliable HRD detection method

Stock problems 5 times

Custom type: structure, enumeration, union
![[add pictures to dice roller app]](/img/36/c5c01946bd8c4ea16ebd3f8f9b92b6.png)
[add pictures to dice roller app]

总结:Prometheus存储

【taichi】在太极中画出规整的网格

LeetCode高频题128. 最长连续序列,经常被互联网大厂面试考到
随机推荐
Discussion on PHP using some functions bypass WAF
BGP联邦实验
Gbase 8C transaction ID and snapshot (VI)
My rich second generation friend
Gbase 8C transaction ID and snapshot
IIC读写EEFPROM
Oracle RAC cluster file directory migration
Collection / container
C # learning notes ----- C # connect to MySQL database
Interview question 01.07. rotation matrix
GBase 8c 事务ID和快照(一)
Linux系统彻底删除Mysql
leetcode: 515. 在每个树行中找最大值
Data warehouse construction - DWS floor
暴雪《暗黑破坏神 4》PS5 / PS4 测试版添加到 PlayStation 数据库
递归相关习题
"Do you" want to be a test / development programmer? We strive to sprout
总结:Prometheus存储
JS数字精度丢失的原因及解决方案
Gbase 8C transaction ID and snapshot (I)