export function tranListToTreeData(list, rootValue) {
// list是最完整的数组
let arr = []; // 记录儿子
list.forEach((item) => {
// 记录是否有儿子
if (item.parentId === rootValue) {
// 继续找自己儿子
const child = tranListToTreeData(list, item.id);
if (child.length) {
item.child = child;
}
// 没有孩子的 直接 push 有孩子的给child 树型
arr.push(item); // 收集儿子
}
});
return arr;
}

定义方法的位置 utlis/index.js  工具层 ;

调用  rootValue 根据 parentID 的最上层父级 确定

tranListToTreeData(list, null)

ps: 使用递归的时候,就是自身调用自身 ,但是条件一定不能相同 ,否则造成死循环 ;

ps:数据类型:

[
{
"id": "1eae3bd8-6425-42c8-90f5-4b73c14fbb73",
"name": "y1",
"sort": 0,
"parentID": "fd215de1-3174-4bff-824a-b496eca31fb4",
"remarks": null,
"createdAt": "2024-05-28T17:01:24.573",
"updatedAt": "2024-05-29T08:57:06.339",
"deletedAt": null
},
{
"id": "251dfdbd-1e48-419f-9be1-a90e3aba6a57",
"name": "y2",
"sort": 1,
"parentID": "fd215de1-3174-4bff-824a-b496eca31fb4",
"remarks": null,
"createdAt": "2024-05-28T17:01:28.234",
"updatedAt": "2024-05-29T08:57:06.644",
"deletedAt": null
},
{
"id": "4e8a3572-18e1-4f00-98c7-cc306195a0b9",
"name": "y4",
"sort": 3,
"parentID": "fd215de1-3174-4bff-824a-b496eca31fb4",
"remarks": null,
"createdAt": "2024-05-28T17:01:35.356",
"updatedAt": "2024-05-29T08:57:06.647",
"deletedAt": null
},
{
"id": "885d0bdc-d03d-4b23-b02b-f3020c7e622f",
"name": "y3",
"sort": 2,
"parentID": "fd215de1-3174-4bff-824a-b496eca31fb4",
"remarks": null,
"createdAt": "2024-05-28T17:01:31.58",
"updatedAt": "2024-05-29T08:57:06.645",
"deletedAt": null
},
{
"id": "fd215de1-3174-4bff-824a-b496eca31fb4",
"name": "yy",
"sort": 1,
"parentID": null,
"remarks": null,
"createdAt": "2024-05-28T16:30:11.244",
"updatedAt": "2024-05-28T16:30:11.244",
"deletedAt": null,
"children": [
{
"id": "1eae3bd8-6425-42c8-90f5-4b73c14fbb73",
"name": "y1",
"sort": 0,
"parentID": "fd215de1-3174-4bff-824a-b496eca31fb4",
"remarks": null,
"createdAt": "2024-05-28T17:01:24.573",
"updatedAt": "2024-05-29T08:57:06.339",
"deletedAt": null
},
{
"id": "251dfdbd-1e48-419f-9be1-a90e3aba6a57",
"name": "y2",
"sort": 1,
"parentID": "fd215de1-3174-4bff-824a-b496eca31fb4",
"remarks": null,
"createdAt": "2024-05-28T17:01:28.234",
"updatedAt": "2024-05-29T08:57:06.644",
"deletedAt": null
},
{
"id": "885d0bdc-d03d-4b23-b02b-f3020c7e622f",
"name": "y3",
"sort": 2,
"parentID": "fd215de1-3174-4bff-824a-b496eca31fb4",
"remarks": null,
"createdAt": "2024-05-28T17:01:31.58",
"updatedAt": "2024-05-29T08:57:06.645",
"deletedAt": null
},
{
"id": "4e8a3572-18e1-4f00-98c7-cc306195a0b9",
"name": "y4",
"sort": 3,
"parentID": "fd215de1-3174-4bff-824a-b496eca31fb4",
"remarks": null,
"createdAt": "2024-05-28T17:01:35.356",
"updatedAt": "2024-05-29T08:57:06.647",
"deletedAt": null
}
]
}
]

2. 使用递归算法将扁平数组转换为树形对象:

const flatData = [
{ id: 1, name: 'Node 1', parentId: null },
{ id: 2, name: 'Node 2', parentId: 1 },
{ id: 3, name: 'Node 3', parentId: 2 },
{ id: 4, name: 'Node 4', parentId: 3 },
{ id: 5, name: 'Node 5', parentId: 3 }
]; function convertToTree(flatData, parentId = null) {
const children = flatData.filter(node => node.parentId === parentId);
if (!children.length) {
return null;
}
return children.map(node => ({
...node,
children: convertToTree(flatData, node.id)
}));
} const treeData = convertToTree(flatData);
console.log(treeData);

将数组数据转化成树形结构 tranListToTreeData的更多相关文章

  1. Vue组件模板形式实现对象数组数据循环为树形结构

    数据结构为数组中包含对象--树形结构,用Vue组件的写法实现以下的效果: 树形列表,缩进显示层级,第5级数据加底色,数据样式显色,点击展开折叠数据.本文为用Vue实现方式,另有一篇为用knockout ...

  2. C# 把带有父子关系的数据转化为------树形结构的数据 ,以及 找出父子级关系的数据中里面的根数据Id

    紧接上一篇,将List<Menu>的扁平结构数据, 转换成树形结构的数据 返回给前端   ,   废话不多说,开撸! --------------------- 步骤: 1. 建 Menu ...

  3. js把json数据转化成树形数据

    /*转化函数*/ function(data, attributes) { let resData = data; let tree = []; for(let i = 0; i < resDa ...

  4. xml格式的数据转化成数组

    将得到的xml格式的数据转化成数组 <?php //构造xml $url = "http://api.map.baidu.com/telematics/v3/weather?locat ...

  5. 使用js将后台返回的数据转换成树形结构

    将类似如下数据转换成树形的数据: [ { id: 1, name: '1', }, { id: 2, name: '1-1', parentId: 1 }, { id: 3, name: '1-1-1 ...

  6. javascript将平行的拥有上下级关系的数据转换成树形结构

    转换函数 var Littlehow = {}; /** * littlehow 2019-05-15 * 平行数据树形转换器 * @type {{format: tree.format, sort: ...

  7. 记一则 Lambda内递归调用方法将集合对象转换成树形结构

    public dynamic GetDepartments(string labID) { List<int> usedIDs = new List<int>(); //缓存已 ...

  8. 将数据转化成字符串时:用字符串的链接 还是 StringBuilder

    /* 目的:将数据转化成字符串时:用字符串的链接 还是 StringBuilder呢? */ public class Test{ public static void main(String[] a ...

  9. c# List列表数据转换成树形结构

    把List列表结构 转换成树形结构 /// <summary> /// 构造树形Json /// </summary> public static class TreeJson ...

  10. 关于mysql中数据存储复合树形结构,查询时结果按树形结构输出

    1.主要思想:根据已有数据,规则性的造数据 select * FROM(select lId,strName,lId as lParentId,-1 as orderIdx from tbClassi ...

随机推荐

  1. 【Vue】Re21 VueX 第二部分(Mutations)

    一.Mutations携带参数处理 Store状态的更新唯一方式:提交Mutations Mutations包含两部分: 1.字符串的事件类型[TYPE] 2.一个回调函数[HANDLER] 该函数的 ...

  2. 【Git】05 分支管理

    查看所有分支: git branch Git将列出所有分支,如果是当前使用的分支,前面会加一个星号表示 创建一个新的分支: git branch 分支名称 创建一个分支并且指向该分支: git che ...

  3. chatgpt的api联网报错问题解决:openai公司的api联网报错解决

    chatgpt是啥,这里不讲,openai是啥这里也不讲.要知道我们不论是通过网页web使用chatgpt还是使用api方式通过客户端使用chatgpt都是需要使用外国IP的, 为啥我们不能访问ope ...

  4. Ubuntu22.04下安装chrome浏览器

    Ubuntu下Chrome的下载: 地址: https://www.google.cn/intl/zh-CN/chrome/ 下载后的文件: 安装: 命令: sudo dpkg -i google-c ...

  5. lua环境配置与编译

    1.背景 2.安装lua 官方下载地址:https://joedf.ahkscript.org/LuaBuilds/ 下载后解压即可 解压后: 配置环境变量: 检查是否安装成功: 如果能输出版本号,则 ...

  6. .NET 轻量化定时任务调度 FreeScheduler

    前言 在平时项目开发中,定时任务调度是一项重要的功能,广泛应用于后台作业.计划任务和自动化脚本等模块. FreeScheduler 是一款轻量级且功能强大的定时任务调度库,它支持临时的延时任务和重复循 ...

  7. C语言编程-GCC编译过程

    gcc编译 预处理 ->编译->汇编->链接 预处理 gcc -E helloworld.c -o helloworld.i 头文件展开:不检查语法错误,即可以展开任意文件: 宏定义 ...

  8. [计算机网络] IPv6

    1 IPv6 概述 引言 近期突发奇想,能不能用 IPv6 的公网地址,给家里的 NAS 做 内网穿透. 技术上是可行的.只是必须确保是 IPv6 的公网地址. 大学学的 IPv6 的知识,早就抛到九 ...

  9. Vue使用v-for 循环生成tabs 标签页

    实现最终效果: template代码: activeName:默认第一个显示的tab <el-tabs v-model="activeName" type="car ...

  10. Standard Quorum Intersection

    标准定足数交集 定义和背景 系统模型: 系统中有 \(n\) 个节点,其中最多 \(f\) 个节点可能是拜占庭故障节点(恶意节点). 为了保证容忍 \(f\) 个拜占庭节点,系统通常需要至少 \(3f ...