当前位置:网站首页>树形数据转换
树形数据转换
2022-07-27 10:25:00 【前端不加班】
/** * 树形数据转换 * @param {*} data * @param {*} id * @param {*} pid */
export function treeDataTranslate (data, id = 'id', pid = 'parentId',order,istyyw,caseId) {
let res = []
let temp = {
}
for (let i = 0; i < data.length; i++) {
temp[data[i][id]] = data[i]
if(istyyw&&caseId){
if(data[i].pid==(caseId+"1")){
temp[data[i][id]]['_level'] = 1;
}
}
}
for (let k = 0; k < data.length; k++) {
if (temp[data[k][pid]] && data[k][id] !== data[k][pid]) {
if (!temp[data[k][pid]]['children']) {
temp[data[k][pid]]['children'] = []
}
if (!temp[data[k][pid]]['_level']) {
temp[data[k][pid]]['_level'] = 1
}
data[k]['_level'] = temp[data[k][pid]]._level + 1
if(istyyw){
data[k]['catalogLevel'] = data[k]['_level'];
}
temp[data[k][pid]]['children'].push(data[k])
} else {
data[k]['_level'] = 1;
if(istyyw){
data[k]['catalogLevel'] = 1;
}
res.push(data[k])
}
}
function orderFunc(a,b){
if(!a[order])a[order] = 0;
if(!b[order])b[order] = 0;
return a[order] - b[order];
}
function sortDataHandle(arr){
arr = arr.sort(orderFunc);
for(var i = 0;i<arr.length;i++){
if(arr[i].children&&arr[i].children.length){
arr[i].children = sortDataHandle(arr[i].children);
}
}
return arr;
}
if(order)res = sortDataHandle(res);
// console.log(res);
return res
}
边栏推荐
- Apache cannot start in phpstudy
- warning package. Json: no license field error
- [brother hero July training] day 16: queue
- Mysql死锁、悲观锁、乐观锁
- Apache22 and opencms9.5 integrated configuration
- 基于Spark封装的二次开发工程edata-base,介绍
- phpstudy中Apache无法启动
- 颜值爆表!推荐两款JSON可视化工具,配合Swagger使用真香
- Detailed explanation of status code meaning
- Learning C language together: structure (2)
猜你喜欢

Want to speed up the vit model with one click? Try this open source tool!

FTP server

gyp ERR! configure error. gyp ERR! stack Error: gyp failed with exit code: 1

Beijing publicized the spot check of 8 batches of children's shoes, and qierte was listed as unqualified

Metasploit Eternal Blue attack

Tcp/ip protocol

DNS principle and resolution process

ASP.NET Core依赖注入之旅:1.理论概念

Multipoint bidirectional republication and routing strategy

Redis数据结构分析(二)
随机推荐
Overview of user space lock on mobile platform
7z用法
JVM -- Analysis of bytecode
Analysis of new communication security risks brought by quantum computer and Countermeasures
One stop monitoring of the software and hardware infrastructure of the whole university, and Suzhou University replaces PostgreSQL with time series database
warning package. Json: no license field error
全校软硬件基础设施一站式监控 ,苏州大学以时序数据库替换 PostgreSQL
It is thought-provoking: is syntax really important? Qiu Xipeng group proposed a powerful baseline for aspect based emotional analysis
异构计算技术分析
ASP. Net core dependency injection journey: 1. Theoretical concepts
Redis data structure analysis (II)
Detailed explanation of status code meaning
TDengine 助力西门子轻量级数字化解决方案 SIMICAS 简化数据处理流程
Mail server
服务器访问速度
Tdengine helps Siemens' lightweight digital solution simicas simplify data processing process
A brief introduction to R language pipeline symbols (% >%) and placeholders (.)
[brother hero's June training] day 27: picture
[Linux] install MySQL
Deep analysis: what is diffusion model?