当前位置:网站首页>Three level menu data implementation, nested three-level menu data
Three level menu data implementation, nested three-level menu data
2022-07-07 05:43:00 【Bitter sea 123】
// Associate the database with (inner join) Query the data of multiple tables that are not nested but have nested relationships for processing , Get the nested parent secondary attribute with nested level, which cannot be repeated , The specific implementation is as follows :
// Query the data in the database through Association ( Format ):
let result=[
{
cid: 1,coll_name: ' Class A 1',pid: 1,projects_name: ' second level 1',qpid:1,common_name:' Level three 1'},
{
cid: 1,coll_name: ' Class A 1',pid: 1,projects_name: ' second level 1',qpid:2,common_name:' Level three 2'},
{
cid: 1,coll_name: ' Class A 1',pid: 2,projects_name: ' second level 2',qpid:3,common_name:' Level three 3'},
{
cid: 1,coll_name: ' Class A 1',pid: 2,projects_name: ' second level 2',qpid:4,common_name:' Level three 4'},
{
cid: 2,coll_name: ' Class A 2',pid: 3,projects_name: ' second level 3',qpid:5,common_name:' Level three 5'},
{
cid: 2,coll_name: ' Class A 2',pid: 3,projects_name: ' second level 3',qpid:6,common_name:' Level three 6'},
{
cid: 2,coll_name: ' Class A 2',pid: 4,projects_name: ' second level 4',qpid:7,common_name:' Level three 7'},
{
cid: 2,coll_name: ' Class A 2',pid: 4,projects_name: ' second level 4',qpid:8,common_name:' Level three 8'}
]
// The final data format :
let da = [
{
cid:1,
coll_name:' Class A 1',
childp:[
{
pid:1,
projects_name:' second level 1',
childq:[
{
qpid:1,
common_name:' Level three 1'
},
{
qpid:2,
common_name:' Level three 2'
},
]
},
{
pid:2,
projects_name:' second level 2',
childq:[
{
qpid:3,
common_name:' Level three 3'
},
{
qpid:4,
common_name:' Level three 4'
},
]
}
]
},
{
cid:2,
coll_name:' Class A 2',
childp:[
{
pid:3,
projects_name:' second level 3',
childq:[
{
qpid:5,
common_name:' Level three 5'
},
{
qpid:6,
common_name:' Level three 6'
},
]
},
{
pid:4,
projects_name:' second level 4',
childq:[
{
qpid:7,
common_name:' Level three 7'
},
{
qpid:8,
common_name:' Level three 8'
},
]
}
]
}
]
// Complete the implementation :
// Final data storage container
let datalist = []
// Save level 1 cid Array of ( Find all non repeating first level menus cid)
let cidarr = [...new Set(result.map(it => it.cid))]
// Process the data of the first level menu : By traversing the first level menu without repetition cidarr Array generates first level menu data , Traverse cidarr It can be determined that there are several first level menu items
cidarr.forEach((cid,ci)=>{
// Generate first level menu items :
let cobj = {
} // First level menu object
let carr = [] // From the original data result Find cid Equal to the current traversal of the first level menu cid The value of the data
result.map(item=>{
// find result in cid Equal to level one cid The data of each value in the array coexists to carr in
if(item.cid===cid){
carr.push(item)
}
})
cobj.cid = cid // Generate first level menu items cid
cobj.coll_name = carr[0].coll_name // Generate first level menu items coll_name
// Handle the value of the secondary menu attribute in the first level :
let pchildtemp = [] // Save the secondary menu data array of the current primary menu
// Find all non duplicate secondary pid
let pidarr = [...new Set(carr.map(itp => itp.pid))]
// According to the non repeated Level 2 of the current level pid Traverse to generate the second level of the current level :
pidarr.map(pid=>{
// Each secondary object
let pobj = {
}
let parr = [] // Save a two-level array
result.map(pitem=>{
// find result in cid Equal to level one id The value of each element in the array
if(pitem.pid===pid){
parr.push(pitem)
}
})
// Save the non repeated Level 3 under the current level 2 qpid
let qidarr = [...new Set(parr.map(pit => pit.qpid))]
// Find the array of the current level pid be equal to pidarr An object of the value in
carr.map(pits=>{
if(pits.pid === pid){
pobj.pid = pits.pid
pobj.projects_name = pits.projects_name
// Process level 3 data :
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)
})
// Print the results :
console.log(datalist)
Print data as shown in the figure below :
Tips : This article, pictures and other materials come from the Internet , If there is infringement , Please send an email to :[email protected] Contact the author to delete .
The author : misery
边栏推荐
- 《HarmonyOS实战—入门到开发,浅析原子化服务》
- An example of multi module collaboration based on NCF
- 4. 对象映射 - Mapping.Mapster
- Hcip eighth operation
- Realize GDB remote debugging function between different network segments
- Tablayout modification of customized tab title does not take effect
- 爬虫练习题(三)
- Dj-zbs2 leakage relay
- 关于服装ERP,你知道多少?
- Message queuing: how to ensure that messages are not lost
猜你喜欢
[binary tree] binary tree path finding
1. AVL tree: left-right rotation -bite
SAP webservice 测试出现404 Not found Service cannot be reached
什么是依赖注入(DI)
Digital innovation driven guide
ssm框架的简单案例
How does mapbox switch markup languages?
How digitalization affects workflow automation
SQL query: subtract the previous row from the next row and make corresponding calculations
Five core elements of architecture design
随机推荐
Batch size setting skills
Educational Codeforces Round 22 B. The Golden Age
Paper reading [MM21 pre training for video understanding challenge:video captioning with pre training techniqu]
得物客服一站式工作台卡顿优化之路
Nodejs get client IP
bat 批示处理详解
Simple case of SSM framework
[JS component] date display.
Flink SQL 实现读写redis,并动态生成Hset key
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
Unity keeps the camera behind and above the player
SQL query: subtract the previous row from the next row and make corresponding calculations
消息队列:消息积压如何处理?
5. Data access - entityframework integration
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
JVM the truth you need to know
Intelligent annotation scheme of entity recognition based on hugging Face Pre training model: generate doccano request JSON format
Senior programmers must know and master. This article explains in detail the principle of MySQL master-slave synchronization, and recommends collecting
Common skills and understanding of SQL optimization
The year of the tiger is coming. Come and make a wish. I heard that the wish will come true