当前位置:网站首页>三级菜单数据实现,实现嵌套三级菜单数据
三级菜单数据实现,实现嵌套三级菜单数据
2022-07-06 23:55:00 【苦海123】
//将数据库中通过关联(inner join)查询多张表没有嵌套但有嵌套关系的数据进行处理,得到具有嵌套层级且嵌套的父级二级属性不可重复出现,具体实现如下:
// 通过关联查询到数据库的数据(格式):
let result=[
{
cid: 1,coll_name: '一级1',pid: 1,projects_name: '二级1',qpid:1,common_name:'三级1'},
{
cid: 1,coll_name: '一级1',pid: 1,projects_name: '二级1',qpid:2,common_name:'三级2'},
{
cid: 1,coll_name: '一级1',pid: 2,projects_name: '二级2',qpid:3,common_name:'三级3'},
{
cid: 1,coll_name: '一级1',pid: 2,projects_name: '二级2',qpid:4,common_name:'三级4'},
{
cid: 2,coll_name: '一级2',pid: 3,projects_name: '二级3',qpid:5,common_name:'三级5'},
{
cid: 2,coll_name: '一级2',pid: 3,projects_name: '二级3',qpid:6,common_name:'三级6'},
{
cid: 2,coll_name: '一级2',pid: 4,projects_name: '二级4',qpid:7,common_name:'三级7'},
{
cid: 2,coll_name: '一级2',pid: 4,projects_name: '二级4',qpid:8,common_name:'三级8'}
]
// 最终要得到的数据格式:
let da = [
{
cid:1,
coll_name:'一级1',
childp:[
{
pid:1,
projects_name:'二级1',
childq:[
{
qpid:1,
common_name:'三级1'
},
{
qpid:2,
common_name:'三级2'
},
]
},
{
pid:2,
projects_name:'二级2',
childq:[
{
qpid:3,
common_name:'三级3'
},
{
qpid:4,
common_name:'三级4'
},
]
}
]
},
{
cid:2,
coll_name:'一级2',
childp:[
{
pid:3,
projects_name:'二级3',
childq:[
{
qpid:5,
common_name:'三级5'
},
{
qpid:6,
common_name:'三级6'
},
]
},
{
pid:4,
projects_name:'二级4',
childq:[
{
qpid:7,
common_name:'三级7'
},
{
qpid:8,
common_name:'三级8'
},
]
}
]
}
]
// 完成实现:
// 最终数据存放容器
let datalist = []
// 存一级cid的数组(找到所有不重复的一级菜单cid)
let cidarr = [...new Set(result.map(it => it.cid))]
// 处理一级菜单的数据:通过遍历循环不重复的一级菜单cidarr数组生成一级菜单数据,遍历cidarr可确定有几个一级菜单项
cidarr.forEach((cid,ci)=>{
// 生成一级菜单项:
let cobj = {
} // 一级菜单对象
let carr = [] // 从原数据result中找到cid等于当前遍历的一级菜单的cid值的数据
result.map(item=>{
// 找出result中cid等于一级cid数组中每项值的数据并存到carr中
if(item.cid===cid){
carr.push(item)
}
})
cobj.cid = cid // 生成一级菜单项的cid
cobj.coll_name = carr[0].coll_name // 生成一级菜单项的coll_name
// 处理一级中的二级菜单属性的值:
let pchildtemp = [] // 存当前一级菜单的二级菜单数据数组
// 找到当前一级中所有不重复的二级的pid
let pidarr = [...new Set(carr.map(itp => itp.pid))]
// 根据当前一级的不重复的二级pid遍历生成当前一级的二级:
pidarr.map(pid=>{
// 每个二级对象
let pobj = {
}
let parr = [] // 存一种二级的数组
result.map(pitem=>{
// 找出result中cid等于一级id数组中每个元素的值
if(pitem.pid===pid){
parr.push(pitem)
}
})
// 存当前二级下不重复的三级qpid
let qidarr = [...new Set(parr.map(pit => pit.qpid))]
// 找出当前一级的数组中pid等于pidarr中的值的某项对象
carr.map(pits=>{
if(pits.pid === pid){
pobj.pid = pits.pid
pobj.projects_name = pits.projects_name
// 处理三级数据:
let qparr = []
qidarr.map(qid=>{
let qpobj = {
}
parr.map(qpit=>{
if(qpit.qpid ===qid){
qpobj.qpid = qpit.qpid
qpobj.common_name = qpit.common_name
}
})
qparr.push(qpobj)
})
pobj.qchild = qparr
}
})
pchildtemp.push(pobj)
})
cobj.pchild = pchildtemp
datalist.push(cobj)
})
// 打印结果:
console.log(datalist)
打印数据如下图:
提示:本文图片等素材来源于网络,若有侵权,请发邮件至邮箱:[email protected]联系笔者删除。
笔者:苦海
边栏推荐
- In memory, I moved from CSDN to blog park!
- ssm框架的简单案例
- 论文阅读【Semantic Tag Augmented XlanV Model for Video Captioning】
- 基于 hugging face 预训练模型的实体识别智能标注方案:生成doccano要求json格式
- AIDL 与Service
- JD commodity details page API interface, JD commodity sales API interface, JD commodity list API interface, JD app details API interface, JD details API interface, JD SKU information interface
- 《2》 Label
- Reading the paper [sensor enlarged egocentric video captioning with dynamic modal attention]
- The navigation bar changes colors according to the route
- [Oracle] simple date and time formatting and sorting problem
猜你喜欢
分布式事务介绍
SAP ABAP BDC(批量数据通信)-018
Is the human body sensor easy to use? How to use it? Which do you buy between aqara green rice and Xiaomi
Preliminary practice of niuke.com (9)
《HarmonyOS实战—入门到开发,浅析原子化服务》
常用消息队列有哪些?
K6el-100 leakage relay
Use Zhiyun reader to translate statistical genetics books
什么是依赖注入(DI)
DOM-节点对象+时间节点 综合案例
随机推荐
[JS component] custom select
Zhang Ping'an: accelerate cloud digital innovation and jointly build an industrial smart ecosystem
sql优化常用技巧及理解
ssm框架的简单案例
Taobao Commodity details page API interface, Taobao Commodity List API interface, Taobao Commodity sales API interface, Taobao app details API interface, Taobao details API interface
Educational Codeforces Round 22 B. The Golden Age
sql查询:将下一行减去上一行,并做相应的计算
张平安:加快云上数字创新,共建产业智慧生态
Différenciation et introduction des services groupés, distribués et microservices
Initial experience of annotation
Tablayout modification of customized tab title does not take effect
Pinduoduo product details interface, pinduoduo product basic information, pinduoduo product attribute interface
Aidl and service
Paper reading [open book video captioning with retrieve copy generate network]
How to get free traffic in pinduoduo new store and what links need to be optimized in order to effectively improve the free traffic in the store
English grammar_ Noun possessive
[PM products] what is cognitive load? How to adjust cognitive load reasonably?
Under the trend of Micah, orebo and apple homekit, how does zhiting stand out?
LabVIEW is opening a new reference, indicating that the memory is full
Lombok插件