js 数据组合,一级结构组合成父子嵌套数组结构

news2025/1/27 23:55:05

1.方法

buildDeptTree(deptData) { //构建树状部门
        // 创建一个 map 来存储 deptId 和对应的部门对象
        const deptMap = new Map();
        // 初始化每个部门对象的 children 属性为空数组
        deptData.forEach(dept => {
            dept.children = [];
            deptMap.set(dept.deptId, dept);
        });
        // 构建树形结构
        let rootDepts = [];
        deptData.forEach(dept => {
            if (dept.parentId === 0) {
                rootDepts.push(dept);
            } else {
                const parentDept = deptMap.get(dept.parentId);
                if (parentDept) {
                    parentDept.children.push(dept);
                }
            }
        });
        // 移除所有 children 为空的数组
        function removeEmptyChildren(depts) {
            depts.forEach(dept => {
                if (dept.children.length === 0) {
                    delete dept.children;
                } else {
                    removeEmptyChildren(dept.children);
                }
            });
        }
        removeEmptyChildren(rootDepts);
        return rootDepts;
    },

2.应用

this.buildDeptTree(res.data);

3.数据:

[
    {
        "createBy": "admin",
        "createTime": "2023-09-06 08:20:03",
        "updateBy": null,
        "updateTime": null,
        "remark": null,
        "deptId": 100,
        "parentId": 0,
        "ancestors": "0",
        "deptName": "普陀",
        "orderNum": 0,
        "leader": "admin",
        "phone": "",
        "email": "",
        "status": "0",
        "delFlag": "0",
        "parentName": null,
        "children": []
    },
    {
        "createBy": "admin",
        "createTime": "2025-01-02 10:43:21",
        "updateBy": null,
        "updateTime": null,
        "remark": null,
        "deptId": 262,
        "parentId": 100,
        "ancestors": "0,100",
        "deptName": "投促办",
        "orderNum": 1,
        "leader": "",
        "phone": "",
        "email": "",
        "status": "0",
        "delFlag": "0",
        "parentName": null,
        "children": []
    },
    {
        "createBy": "admin",
        "createTime": "2025-01-18 11:20:17",
        "updateBy": null,
        "updateTime": null,
        "remark": null,
        "deptId": 312,
        "parentId": 100,
        "ancestors": "0,100",
        "deptName": "委办",
        "orderNum": 1,
        "leader": "",
        "phone": "",
        "email": "",
        "status": "0",
        "delFlag": "0",
        "parentName": null,
        "children": []
    },
    {
        "createBy": "admin",
        "createTime": "2023-09-06 08:20:03",
        "updateBy": null,
        "updateTime": null,
        "remark": null,
        "deptId": 101,
        "parentId": 100,
        "ancestors": "0,100",
        "deptName": "上海联通分公司",
        "orderNum": 1,
        "leader": "若依",
        "phone": "15888888888",
        "email": "ry@qq.com",
        "status": "0",
        "delFlag": "0",
        "parentName": null,
        "children": []
    },
    {
        "createBy": "admin",
        "createTime": "2024-12-20 14:41:07",
        "updateBy": null,
        "updateTime": null,
        "remark": null,
        "deptId": 232,
        "parentId": 100,
        "ancestors": "0,100",
        "deptName": "诉求",
        "orderNum": 1,
        "leader": "",
        "phone": "",
        "email": "",
        "status": "0",
        "delFlag": "0",
        "parentName": null,
        "children": []
    },
    {
        "createBy": "admin",
        "createTime": "2025-01-02 10:51:51",
        "updateBy": null,
        "updateTime": null,
        "remark": null,
        "deptId": 266,
        "parentId": 100,
        "ancestors": "0,100",
        "deptName": "街道",
        "orderNum": 1,
        "leader": "",
        "phone": "",
        "email": "",
        "status": "0",
        "delFlag": "0",
        "parentName": null,
        "children": []
    },
    {
        "createBy": "admin",
        "createTime": "2023-10-17 14:25:10",
        "updateBy": null,
        "updateTime": null,
        "remark": null,
        "deptId": 210,
        "parentId": 100,
        "ancestors": "0,100",
        "deptName": "XX联通分公司",
        "orderNum": 2,
        "leader": "",
        "phone": "",
        "email": "",
        "status": "0",
        "delFlag": "0",
        "parentName": null,
        "children": []
    },
    {
        "createBy": "admin",
        "createTime": "2023-09-06 08:20:03",
        "updateBy": null,
        "updateTime": null,
        "remark": null,
        "deptId": 103,
        "parentId": 101,
        "ancestors": "0,100,101",
        "deptName": "研发部门",
        "orderNum": 1,
        "leader": "若依",
        "phone": "15888888888",
        "email": "ry@qq.com",
        "status": "0",
        "delFlag": "0",
        "parentName": null,
        "children": []
    },
    {
        "createBy": "admin",
        "createTime": "2023-09-06 08:20:03",
        "updateBy": null,
        "updateTime": null,
        "remark": null,
        "deptId": 105,
        "parentId": 101,
        "ancestors": "0,100,101",
        "deptName": "测试部门",
        "orderNum": 2,
        "leader": "若依",
        "phone": "15888888888",
        "email": "ry@qq.com",
        "status": "0",
        "delFlag": "0",
        "parentName": null,
        "children": []
    },
    {
        "createBy": "admin",
        "createTime": "2024-12-20 14:41:40",
        "updateBy": null,
        "updateTime": null,
        "remark": null,
        "deptId": 243,
        "parentId": 232,
        "ancestors": "0,100,232",
        "deptName": "诉求协办部门",
        "orderNum": 1,
        "leader": "",
        "phone": "",
        "email": "",
        "status": "0",
        "delFlag": "0",
        "parentName": null,
        "children": []
    },
    {
        "createBy": "admin",
        "createTime": "2024-12-20 14:41:29",
        "updateBy": null,
        "updateTime": null,
        "remark": null,
        "deptId": 242,
        "parentId": 232,
        "ancestors": "0,100,232",
        "deptName": "诉求会办部门",
        "orderNum": 1,
        "leader": "",
        "phone": "",
        "email": "",
        "status": "0",
        "delFlag": "0",
        "parentName": null,
        "children": []
    },
    {
        "createBy": "admin",
        "createTime": "2024-12-20 14:41:20",
        "updateBy": null,
        "updateTime": null,
        "remark": null,
        "deptId": 233,
        "parentId": 232,
        "ancestors": "0,100,232",
        "deptName": "诉求主办部门",
        "orderNum": 1,
        "leader": "",
        "phone": "",
        "email": "",
        "status": "0",
        "delFlag": "0",
        "parentName": null,
        "children": []
    },
    {
        "createBy": "admin",
        "createTime": "2025-01-02 16:34:55",
        "updateBy": null,
        "updateTime": null,
        "remark": null,
        "deptId": 303,
        "parentId": 262,
        "ancestors": "0,100,262",
        "deptName": "桃浦地区投促中心",
        "orderNum": 2,
        "leader": "",
        "phone": "",
        "email": "",
        "status": "0",
        "delFlag": "0",
        "parentName": null,
        "children": []
    },
    {
        "createBy": "admin",
        "createTime": "2025-01-02 16:35:09",
        "updateBy": null,
        "updateTime": null,
        "remark": null,
        "deptId": 304,
        "parentId": 262,
        "ancestors": "0,100,262",
        "deptName": "真如地区投促中心",
        "orderNum": 2,
        "leader": "",
        "phone": "",
        "email": "",
        "status": "0",
        "delFlag": "0",
        "parentName": null,
        "children": []
    },
    {
        "createBy": "admin",
        "createTime": "2025-01-02 16:35:18",
        "updateBy": null,
        "updateTime": null,
        "remark": null,
        "deptId": 305,
        "parentId": 262,
        "ancestors": "0,100,262",
        "deptName": "长风地区投促中心",
        "orderNum": 2,
        "leader": "",
        "phone": "",
        "email": "",
        "status": "0",
        "delFlag": "0",
        "parentName": null,
        "children": []
    },
    {
        "createBy": "admin",
        "createTime": "2025-01-02 16:35:27",
        "updateBy": null,
        "updateTime": null,
        "remark": null,
        "deptId": 306,
        "parentId": 262,
        "ancestors": "0,100,262",
        "deptName": "长征地区投促中心",
        "orderNum": 2,
        "leader": "",
        "phone": "",
        "email": "",
        "status": "0",
        "delFlag": "0",
        "parentName": null,
        "children": []
    },
    {
        "createBy": "admin",
        "createTime": "2025-01-02 10:47:25",
        "updateBy": null,
        "updateTime": null,
        "remark": null,
        "deptId": 263,
        "parentId": 262,
        "ancestors": "0,100,262",
        "deptName": "项目推进科",
        "orderNum": 2,
        "leader": "",
        "phone": "",
        "email": "",
        "status": "0",
        "delFlag": "0",
        "parentName": null,
        "children": []
    },
    {
        "createBy": "admin",
        "createTime": "2025-01-02 10:47:36",
        "updateBy": null,
        "updateTime": null,
        "remark": null,
        "deptId": 272,
        "parentId": 262,
        "ancestors": "0,100,262",
        "deptName": "企业服务科",
        "orderNum": 2,
        "leader": "",
        "phone": "",
        "email": "",
        "status": "0",
        "delFlag": "0",
        "parentName": null,
        "children": []
    },
    {
        "createBy": "admin",
        "createTime": "2025-01-02 10:47:47",
        "updateBy": null,
        "updateTime": null,
        "remark": null,
        "deptId": 273,
        "parentId": 262,
        "ancestors": "0,100,262",
        "deptName": "地区指导科",
        "orderNum": 2,
        "leader": "",
        "phone": "",
        "email": "",
        "status": "0",
        "delFlag": "0",
        "parentName": null,
        "children": []
    },
    {
        "createBy": "admin",
        "createTime": "2025-01-02 10:50:39",
        "updateBy": null,
        "updateTime": null,
        "remark": null,
        "deptId": 264,
        "parentId": 262,
        "ancestors": "0,100,262",
        "deptName": "经济运行科",
        "orderNum": 2,
        "leader": "",
        "phone": "",
        "email": "",
        "status": "0",
        "delFlag": "0",
        "parentName": null,
        "children": []
    },
    {
        "createBy": "admin",
        "createTime": "2025-01-02 10:50:22",
        "updateBy": null,
        "updateTime": null,
        "remark": null,
        "deptId": 282,
        "parentId": 262,
        "ancestors": "0,100,262",
        "deptName": "办公室主任",
        "orderNum": 2,
        "leader": "",
        "phone": "",
        "email": "",
        "status": "0",
        "delFlag": "0",
        "parentName": null,
        "children": []
    },
    {
        "createBy": "admin",
        "createTime": "2025-01-02 10:51:30",
        "updateBy": null,
        "updateTime": null,
        "remark": null,
        "deptId": 265,
        "parentId": 262,
        "ancestors": "0,100,262",
        "deptName": "综合科",
        "orderNum": 2,
        "leader": "",
        "phone": "",
        "email": "",
        "status": "0",
        "delFlag": "0",
        "parentName": null,
        "children": []
    },
    {
        "createBy": "admin",
        "createTime": "2025-01-02 10:51:39",
        "updateBy": null,
        "updateTime": null,
        "remark": null,
        "deptId": 274,
        "parentId": 262,
        "ancestors": "0,100,262",
        "deptName": "产业发展科",
        "orderNum": 2,
        "leader": "",
        "phone": "",
        "email": "",
        "status": "0",
        "delFlag": "0",
        "parentName": null,
        "children": []
    },
    {
        "createBy": "admin",
        "createTime": "2025-01-02 16:34:42",
        "updateBy": null,
        "updateTime": null,
        "remark": null,
        "deptId": 302,
        "parentId": 262,
        "ancestors": "0,100,262",
        "deptName": "长寿地区投促中心",
        "orderNum": 2,
        "leader": "",
        "phone": "",
        "email": "",
        "status": "0",
        "delFlag": "0",
        "parentName": null,
        "children": []
    },
    {
        "createBy": "admin",
        "createTime": "2025-01-02 10:52:56",
        "updateBy": null,
        "updateTime": null,
        "remark": null,
        "deptId": 285,
        "parentId": 266,
        "ancestors": "0,100,266",
        "deptName": "石泉路街道",
        "orderNum": 2,
        "leader": "",
        "phone": "",
        "email": "",
        "status": "0",
        "delFlag": "0",
        "parentName": null,
        "children": []
    },
    {
        "createBy": "admin",
        "createTime": "2025-01-02 10:52:30",
        "updateBy": null,
        "updateTime": null,
        "remark": null,
        "deptId": 292,
        "parentId": 266,
        "ancestors": "0,100,266",
        "deptName": "长寿路街道",
        "orderNum": 2,
        "leader": "",
        "phone": "",
        "email": "",
        "status": "0",
        "delFlag": "0",
        "parentName": null,
        "children": []
    },
    {
        "createBy": "admin",
        "createTime": "2025-01-02 10:52:39",
        "updateBy": null,
        "updateTime": null,
        "remark": null,
        "deptId": 283,
        "parentId": 266,
        "ancestors": "0,100,266",
        "deptName": "曹杨路街道",
        "orderNum": 2,
        "leader": "",
        "phone": "",
        "email": "",
        "status": "0",
        "delFlag": "0",
        "parentName": null,
        "children": []
    },
    {
        "createBy": "admin",
        "createTime": "2025-01-02 10:52:47",
        "updateBy": null,
        "updateTime": null,
        "remark": null,
        "deptId": 284,
        "parentId": 266,
        "ancestors": "0,100,266",
        "deptName": "宜川路街道",
        "orderNum": 2,
        "leader": "",
        "phone": "",
        "email": "",
        "status": "0",
        "delFlag": "0",
        "parentName": null,
        "children": []
    },
    {
        "createBy": "admin",
        "createTime": "2025-01-02 10:53:03",
        "updateBy": null,
        "updateTime": null,
        "remark": null,
        "deptId": 286,
        "parentId": 266,
        "ancestors": "0,100,266",
        "deptName": "甘泉路街道",
        "orderNum": 2,
        "leader": "",
        "phone": "",
        "email": "",
        "status": "0",
        "delFlag": "0",
        "parentName": null,
        "children": []
    },
    {
        "createBy": "admin",
        "createTime": "2025-01-02 10:53:11",
        "updateBy": null,
        "updateTime": null,
        "remark": null,
        "deptId": 287,
        "parentId": 266,
        "ancestors": "0,100,266",
        "deptName": "真如镇街道",
        "orderNum": 2,
        "leader": "",
        "phone": "",
        "email": "",
        "status": "0",
        "delFlag": "0",
        "parentName": null,
        "children": []
    },
    {
        "createBy": "admin",
        "createTime": "2025-01-02 10:53:17",
        "updateBy": null,
        "updateTime": null,
        "remark": null,
        "deptId": 288,
        "parentId": 266,
        "ancestors": "0,100,266",
        "deptName": "万里街道",
        "orderNum": 2,
        "leader": "",
        "phone": "",
        "email": "",
        "status": "0",
        "delFlag": "0",
        "parentName": null,
        "children": []
    },
    {
        "createBy": "admin",
        "createTime": "2025-01-02 10:53:25",
        "updateBy": null,
        "updateTime": null,
        "remark": null,
        "deptId": 289,
        "parentId": 266,
        "ancestors": "0,100,266",
        "deptName": "长风街道",
        "orderNum": 2,
        "leader": "",
        "phone": "",
        "email": "",
        "status": "0",
        "delFlag": "0",
        "parentName": null,
        "children": []
    },
    {
        "createBy": "admin",
        "createTime": "2025-01-02 10:53:34",
        "updateBy": null,
        "updateTime": null,
        "remark": null,
        "deptId": 290,
        "parentId": 266,
        "ancestors": "0,100,266",
        "deptName": "长征镇",
        "orderNum": 2,
        "leader": "",
        "phone": "",
        "email": "",
        "status": "0",
        "delFlag": "0",
        "parentName": null,
        "children": []
    },
    {
        "createBy": "admin",
        "createTime": "2025-01-02 10:53:43",
        "updateBy": null,
        "updateTime": null,
        "remark": null,
        "deptId": 291,
        "parentId": 266,
        "ancestors": "0,100,266",
        "deptName": "桃浦镇",
        "orderNum": 2,
        "leader": "",
        "phone": "",
        "email": "",
        "status": "0",
        "delFlag": "0",
        "parentName": null,
        "children": []
    },
    {
        "createBy": "admin",
        "createTime": "2025-01-18 11:20:28",
        "updateBy": null,
        "updateTime": null,
        "remark": null,
        "deptId": 313,
        "parentId": 312,
        "ancestors": "0,100,312",
        "deptName": "财政局",
        "orderNum": 1,
        "leader": "",
        "phone": "",
        "email": "",
        "status": "0",
        "delFlag": "0",
        "parentName": null,
        "children": []
    }
]

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/2283395.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

Python GUI 开发 | PySide6 PyQt6 学习手册

本文是个 Python GUI 开发的目录,方便读者系统性学习的,笔者后续会满满填充此目录中的内容,感兴趣的小伙伴可以关注一手。(主要是偏向 PySide6 方向的) 0x01:PySide6 & PyQt6 基础入门 0x0101&#xff…

扣子平台音频功能:让声音也能“智能”起来

在数字化时代,音频内容的重要性不言而喻。无论是在线课程、有声读物,还是各种多媒体应用,音频都是传递信息、增强体验的关键元素。扣子平台的音频功能,为开发者和内容创作者提供了一个强大而灵活的工具,让音频的使用和…

mongoDB常见指令

即使我们自己开发用不到mongoDB,但是接手别人项目的时候,别人如果用了,我们也要会简单调试一下 虽然mongoDB用的不是sql语句,但语句的逻辑都是相似的,比如查看数据库、数据表,增删改查这些 我们下面以doc…

实验二 数据库的附加/分离、导入/导出与备份/还原

实验二 数据库的附加/分离、导入/导出与备份/还原 一、实验目的 1、理解备份的基本概念,掌握各种备份数据库的方法。 2、掌握如何从备份中还原数据库。 3、掌握数据库中各种数据的导入/导出。 4、掌握数据库的附加与分离,理解数据库的附加与分离的作用。…

RubyFPV开源代码之系统简介

RubyFPV开源代码之系统简介 1. 源由2. 工程架构3. 特性介绍(软件)3.1 特性亮点3.2 数字优势3.3 使用功能 4. DEMO推荐(硬件)4.1 天空端4.2 地面端4.3 按键硬件Raspberry PiRadxa 3W/E/C 5. 软件设计6. 参考资料 1. 源由 RubyFPV以…

php代码审计2 piwigo CMS in_array()函数漏洞

php代码审计2 piwigo CMS in_array()函数漏洞 一、目的 本次学习目的是了解in_array()函数和对项目piwigo中关于in_array()函数存在漏洞的一个审计并利用漏洞获得管理员帐号。 二、in_array函数学习 in_array() 函数搜索数组中是否存在指定的值。 in_array($search,$array…

PHP校园助手系统小程序

🔑 校园助手系统 —— 智慧校园生活 📱一款基于ThinkPHPUniapp框架深度定制的校园助手系统,犹如一把智慧之钥,专为校园团队精心打造,解锁智慧校园生活的无限精彩。它独家适配微信小程序,无需繁琐的下载与安…

【Numpy核心编程攻略:Python数据处理、分析详解与科学计算】1.2 ndarray解剖课:多维数组的底层实现

1.2 《ndarray解剖课:多维数组的底层实现》 内容介绍 NumPy 的 ndarray 是其核心数据结构,用于高效处理多维数组。在这篇文章中,我们将深入解析 ndarray 的底层实现,探讨其内存结构、维度、数据类型、步长等关键概念&#xff0c…

计算机网络 (61)移动IP

前言 移动IP(Mobile IP)是由Internet工程任务小组(Internet Engineering Task Force,IETF)提出的一个协议,旨在解决移动设备在不同网络间切换时的通信问题,确保移动设备可以在离开原有网络或子网…

css粘性定位超出指定宽度失效问题

展示效果 解决办法&#xff1a;外层容器添加display:grid即可 完整代码 <template><div class"box"><div class"line" v-for"items in 10"><div class"item" v-for"item in 8">drgg</div>&…

10 Hyperledger Fabric 介绍

简介 HypeLedger&#xff08;超级账本&#xff09;是由Linux基金会2015年创建的首个面向企业应用场景的开源分布式账本平台。 HypeLedger Fabric是HypeLedger种的区块链项目之一HypeLedger Fabric引入权限管理在架构设计上支持可插拔、可扩展是首个面向联盟链场景的开源项目 …

分布式光纤应变监测是一种高精度、分布式的监测技术

一、土木工程领域 桥梁结构健康监测 主跨应变监测&#xff1a;在大跨度桥梁的主跨部分&#xff0c;如悬索桥的主缆、斜拉桥的斜拉索和主梁&#xff0c;分布式光纤应变传感器可以沿着这些关键结构部件进行铺设。通过实时监测应变情况&#xff0c;能够精确捕捉到车辆荷载、风荷…

机器学习10-解读CNN代码Pytorch版

机器学习10-解读CNN代码Pytorch版 我个人是Java程序员&#xff0c;关于Python代码的使用过程中的相关代码事项&#xff0c;在此进行记录 文章目录 机器学习10-解读CNN代码Pytorch版1-核心逻辑脉络2-参考网址3-解读CNN代码Pytorch版本1-MNIST数据集读取2-CNN网络的定义1-无注释版…

【C++高并发服务器WebServer】-7:共享内存

本文目录 一、共享内存1.1 shmget函数1.2 shmat1.3 shmdt1.4 shmctl1.5 ftok1.6 共享内存和内存映射的关联1.7 小demo 二、共享内存操作命令 一、共享内存 共享内存允许两个或者多个进程共享物理内存的同一块区域&#xff08;通常被称为段&#xff09;。由于一个共享内存段会称…

稀土抗菌剂:提升产品质量,保障公共健康

随着全球对抗菌技术需求的不断增长&#xff0c;传统的抗菌剂逐渐暴露出其局限性&#xff0c;包括耐药性、环境污染及副作用等问题。在此背景下&#xff0c;稀土抗菌剂作为一种新兴的抗菌材料&#xff0c;凭借其卓越的抗菌性能、环保特性以及应用多样性&#xff0c;正在成为各行…

机器学习11-学习路径推荐

机器学习11-学习路径推荐 本文希望摒除AI学习商业宣传要素&#xff0c;推荐一条极简的AI学习路线&#xff01;推荐内容均为在线免费内容&#xff0c;如果有条件可以咨询专业的培训机构&#xff01; 文章目录 机器学习11-学习路径推荐[toc] 1-AI培训路线第一阶段 Python-人工智能…

《边界感知的分而治之方法:基于扩散模型的无监督阴影去除解决方案》学习笔记

paper&#xff1a;Boundary-Aware Divide and Conquer: A Diffusion-Based Solution for Unsupervised Shadow Removal 目录 摘要 1、介绍 2、相关工作 2.1 阴影去除 2.2 去噪扩散概率模型&#xff08;Denoising Diffusion Probabilistic Models, DDPM&#xff09; 3、方…

java后端之事务管理

Transactional注解&#xff1a;作用于业务层的方法、类、接口上&#xff0c;将当前方法交给spring进行事务管理&#xff0c;执行前开启事务&#xff0c;成功执行则提交事务&#xff0c;执行异常回滚事务 spring事务管理日志&#xff1a; 默认情况下&#xff0c;只有出现Runti…

数据结构——概念与时间空间复杂度

目录 前言 一相关概念 1什么是数据结构 2什么是算法 二算法效率 1如何衡量算法效率的好坏 2算法的复杂度 三时间复杂度 1时间复杂度表示 2计算时间复杂度 2.1题一 2.2题二 2.3题三 2.4题四 2.5题五 2.6题六 2.7题七 2.8题八 四空间复杂度 1题一 2题二 3…

牛客周赛 Round 78 A-C

A.时间表查询&#xff01; 链接&#xff1a;https://ac.nowcoder.com/acm/contest/100671/A 来源&#xff1a;牛客网 题目描述 今天是2025年1月25日&#xff0c;今年的六场牛客寒假算法基础集训营中&#xff0c;前两场比赛已经依次于 20250121、20250123 举行&#xff1b;而…