当前位置:网站首页>树形数据转换
树形数据转换
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
}
边栏推荐
- Open source project - taier1.2 release, new workflow, tenant binding simplification and other functions
- File upload vulnerability bypass method
- Learning C language together: structure (2)
- 颜值爆表!推荐两款JSON可视化工具,配合Swagger使用真香
- Solved syntaxerror: (Unicode error) 'Unicode scape' codec can't decode bytes in position 2-3: truncated
- Multipoint bidirectional republication and routing strategy
- Server access speed
- Share machine learning notes (PDF version) + practical projects (dataset + code)
- Voice data acquisition - real time voice data visualization
- Tcp/ip protocol
猜你喜欢

Set up Samba service

WEB服务如何平滑的上下线

The difference between scalar, vector, matrix and tensor in deep learning

Metasploit Eternal Blue attack

Use__ slots__ And__ dict__ To save space (it's simply a qualitative leap, and leetcode's personal test is effective)

MySQL index, transaction and storage engine

Tdengine business ecosystem partner recruitment starts

One stop monitoring of the software and hardware infrastructure of the whole university, and Suzhou University replaces PostgreSQL with time series database

Your appearance is amazing! Two JSON visualization tools are recommended for use with swagger. It's really fragrant

TDengine 助力西门子轻量级数字化解决方案 SIMICAS 简化数据处理流程
随机推荐
Family Trivia
R语言管道符(%>%)及占位(.)的简单介绍
Detailed explanation of status code meaning
多点双向重发布和路由策略
让人深思:句法真的重要吗?邱锡鹏组提出一种基于Aspect的情感分析的强大基线...
File upload vulnerability related
厉害了!VMware ESXi安装记录,附下载
深度解析:什么是Diffusion Model?
Tdengine business ecosystem partner recruitment starts
PHP generates text and image watermarks
这种动态规划你见过吗——状态机动态规划之股票问题(上)
Voice data acquisition - real time voice data visualization
A few simple steps to realize the sharing network for industrial raspberry pie
Mail server
WEB服务如何平滑的上下线
Deep analysis: what is diffusion model?
Symmetric encryption and asymmetric encryption
A brief introduction to R language pipeline symbols (% >%) and placeholders (.)
Pyqt5 rapid development and practice 4.2 QWidget
flask_restful中的输出域(Resource、fields、marshal、marshal_with)