当前位置:网站首页>树形数据转换
树形数据转换
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
}
边栏推荐
- Analysis of new communication security risks brought by quantum computer and Countermeasures
- Metaspolit
- Matlab create the logo of MATLAB
- 全校软硬件基础设施一站式监控 ,苏州大学以时序数据库替换 PostgreSQL
- WebRTC实现简单音视频通话功能
- [brother hero June training] day 24: line segment tree
- Open source project - taier1.2 release, new workflow, tenant binding simplification and other functions
- Tensorflow notes - basic functions and concepts
- flask_ Output fields in restful (resources, fields, marshal, marshal_with)
- 简单几步教您实现为工业树莓派共享网络
猜你喜欢

jvm--字节码浅析

Apache cannot start in phpstudy

家庭琐事问题

Server access speed

Views, triggers and stored procedures in MySQL

让人深思:句法真的重要吗?邱锡鹏组提出一种基于Aspect的情感分析的强大基线...

Codeforces Round #807 (Div 2.) AB

Tdengine business ecosystem partner recruitment starts
![[Flink] Flink builds clusters in standalone mode](/img/5b/e566fdd2792b5cda7d37d308ee32e2.png)
[Flink] Flink builds clusters in standalone mode

Alibaba mailbox web login turn processing
随机推荐
Family Trivia
分享机器学习笔记(PDF版)+实战项目(数据集+代码)
Review and Prospect of encrypted traffic identification based on deep learning
Distributed block device replication: client
Shardingsphere kernel principle
Matlab- draw date and duration diagram
MySQL log management, backup and recovery
让人深思:句法真的重要吗?邱锡鹏组提出一种基于Aspect的情感分析的强大基线...
YonBuilder赋能创新,用友第四届开发者大赛“金键盘奖”开启竞逐!
多点双向重发布和路由策略
想要一键加速ViT模型?试试这个开源工具!
One stop monitoring of the software and hardware infrastructure of the whole university, and Suzhou University replaces PostgreSQL with time series database
服务器访问速度
MySQL数据表的高级操作
The largest square of leetcode (violence solving and dynamic programming solving)
NodeJS中Error: getaddrinfo ENOTFOUND localhost
Your appearance is amazing! Two JSON visualization tools are recommended for use with swagger. It's really fragrant
Delete in MySQL: the difference between delete, drop and truncate
No Identifier specified for entity的解决办法
Establishment of NFS server