当前位置:网站首页>JS: 数组和树的相互转换
JS: 数组和树的相互转换
2022-08-04 18:45:00 【你吃香蕉吗?】
一、数组转树
let arr = [{
"categoryCode": "90000",
"categoryCnName": "A1级别",
"bdictCode": "111",
"bdictDesc": "测试1",
"bparentCode": "0",
"validStatus": "1"
}, {
"categoryCode": "90000",
"categoryCnName": "A1级别",
"bdictCode": "222",
"bdictDesc": "测试2",
"bparentCode": "0",
"validStatus": "1"
}, {
"categoryCode": "90000",
"categoryCnName": "A1级别",
"bdictCode": "333",
"bdictDesc": "测试3",
"bparentCode": "0",
"validStatus": "1"
}, {
"categoryCode": "90000",
"categoryCnName": "A1级别",
"bdictCode": "444",
"bdictDesc": "测试4",
"bparentCode": "0",
"validStatus": "1"
}, {
"categoryCode": "90000",
"categoryCnName": "A1级别",
"bdictCode": "555",
"bdictDesc": "测试5",
"bparentCode": "0",
"validStatus": "1"
}, {
"categoryCode": "90000",
"categoryCnName": "A1级别",
"bdictCode": "DOCA",
"bdictDesc": "测试6",
"bparentCode": "0",
"validStatus": "1"
}, {
"categoryCode": "90000",
"categoryCnName": "A1级别",
"bdictCode": "DOCA01",
"bdictDesc": "测试7",
"bparentCode": "DOCA",
"validStatus": "1"
}, {
"categoryCode": "90000",
"categoryCnName": "A1级别",
"bdictCode": "DOCA0101",
"bdictDesc": "测试8",
"bparentCode": "DOCA01",
"validStatus": "1"
}, {
"categoryCode": "90000",
"categoryCnName": "A1级别",
"bdictCode": "DOCA0102",
"bdictDesc": "测试9",
"bparentCode": "DOCA01",
"validStatus": "1"
}, {
"categoryCode": "90000",
"categoryCnName": "A1级别",
"bdictCode": "DOCA0103",
"bdictDesc": "测试10",
"bparentCode": "DOCA01",
"validStatus": "1"
}, {
"categoryCode": "90000",
"categoryCnName": "A1级别",
"bdictCode": "DOCA02",
"bdictDesc": "测试11",
"bparentCode": "DOCA",
"validStatus": "1"
}, {
"categoryCode": "90000",
"categoryCnName": "A1级别",
"bdictCode": "DOCA03",
"bdictDesc": "测试12",
"bparentCode": "DOCA",
"validStatus": "1"
}, {
"categoryCode": "90000",
"categoryCnName": "A1级别",
"bdictCode": "DOCA0301",
"bdictDesc": "测试13",
"bparentCode": "DOCA03",
"validStatus": "1"
}, {
"categoryCode": "90000",
"categoryCnName": "A1级别",
"bdictCode": "DOCA0302",
"bdictDesc": "测试14",
"bparentCode": "DOCA03",
"validStatus": "1"
}, {
"categoryCode": "90000",
"categoryCnName": "A1级别",
"bdictCode": "DOCA04",
"bdictDesc": "测试15",
"bparentCode": "DOCA",
"validStatus": "1"
}, {
"categoryCode": "90000",
"categoryCnName": "A1级别",
"bdictCode": "DOCA05",
"bdictDesc": "测试16",
"bparentCode": "DOCA",
"validStatus": "1"
}, {
"categoryCode": "90000",
"categoryCnName": "A1级别",
"bdictCode": "DOCA06",
"bdictDesc": "测试17",
"bparentCode": "DOCA",
"validStatus": "1"
}, {
"categoryCode": "90000",
"categoryCnName": "A1级别",
"bdictCode": "DOCA07",
"bdictDesc": "测试18",
"bparentCode": "DOCA",
"validStatus": "1"
}, {
"categoryCode": "90000",
"categoryCnName": "A1级别",
"bdictCode": "DOCA08",
"bdictDesc": "测试19",
"bparentCode": "DOCA",
"validStatus": "1"
}, {
"categoryCode": "90000",
"categoryCnName": "A1级别",
"bdictCode": "DOCA09",
"bdictDesc": "测试20",
"bparentCode": "DOCA",
"validStatus": "1"
}, {
"categoryCode": "90000",
"categoryCnName": "A1级别",
"bdictCode": "DOCA10",
"bdictDesc": "测试21",
"bparentCode": "DOCA",
"validStatus": "1"
}, ]
function arrayToTree(list, parentID) {
const child = function(pareID) {
//先定义一个数组,用于存储所查到的子元素
const childs = [];
//循环数组
for (let i = 0; i < list.length; i++) {
//如果数组其中一项的bparentCode等于传入的,说明这一项是传入的子元素,把他push进数组,然后重复递归自己找该项的子元素
if (list[i].bparentCode === pareID) {
if (child(list[i].bdictCode).length > 0) {
list[i].children = child(list[i].bdictCode);
}
childs.push(list[i]);
}
}
//最后将查到的所有子元素返回
return childs;
};
return child(parentID);
}
let res = arrayToTree(arr, '0')
console.log(res);
二、树转数组
树转数组,实现的要点还是在于递归查找,定义一个空数组,去接收没有children属性的每一项
let treeRes = [];
function tree2arr(res) {
res.forEach(item => {
if (!item.children) {
treeRes.push(item)
} else {
tree2arr(item.children)
}
})
}
// 此处的res取的是上边的数组转树的结果
tree2arr(res)
console.log(treeRes);
边栏推荐
- Regardless of whether you are a public, professional or non-major class, I have been sorting out the learning route for a long time here, and the learning route I have summarized is not yet rolled up
- 智能视频监控平台EasyCVR如何使用接口批量导出iframe地址?
- 基于3D机器视觉的采血试管分拣系统
- 基于 eBPF 的 Kubernetes 可观测实践
- vantui 组件 van-field 路由切换时,字体样式混乱问题
- win10 uwp json
- CIFAR发布《AI伦理的文化:研讨会报告》【附下载】
- 【web自动化测试】playwright安装失败怎么办
- 如何进行自动化测试?
- EasyCVR如何通过接口调用设备录像的倍速回放?
猜你喜欢
随机推荐
JS 问号?妙用
使用.NET简单实现一个Redis的高性能克隆版(二)
Google Earth Engine APP——一键在线查看全球1984-至今年的影像同时加载一个影像分析
浅谈web网站架构演变过程
Route lazy loading
单行、多行文本超出显示省略号
EuROC dataset format and related codes
【填空题】130道面试填空题
2018年南海区小学生程序设计竞赛详细答案
margin 塌陷和重合的理解
Google Earth Engine APP - one-click online viewing of global images from 1984 to this year and loading an image analysis at the same time
EasyCVR本地接入国标设备映射公网后,本地设备出现无法播放与级联的解决方法
unity中实现ue眼球的渲染
mq消息积压怎么对应
网站设计师:Nicepage 4.15 Crack By Xacker
win10 uwp win2d 使用 Path 绘制界面
Hezhou Cat1 4G module Air724UG is configured with RNDIS network card or PPP dial-up, and the development board is connected to the Internet through the RNDIS network card (taking the RV1126/1109 devel
【AI+医疗】斯坦福大学最新博士论文《深度学习在医学影像理解中的应用》,205页pdf
#yyds干货盘点# 面试必刷TOP101:链表相加(二)
如何进行自动化测试?