【JS】扁平树数据转为树结构

扁平数据

转为最终效果

js 复制代码
[{"label":"疼逊有限公司","code":"1212","disabled":false,"parentId":"none","children":[{"label":"财务部","code":"34343","disabled":false,"parentId":"1212"},{"label":"人事部","code":"43434","disabled":false,"parentId":"1212"},{"label":"经营 部","code":"53543","disabled":false,"parentId":"1212","children":[{"label":"市场部","code":"23232","disabled":true,"parentId":"53543"},{"label":"销售部","code":"3435435","disabled":false,"parentId":"53543"}]}]}]

js 复制代码
/**
 * 删除子级数据为空的子级
 * @param {[]} node 
 * @param {string} [childKey='children'] 子级字段 默认-children
 * @returns {[]}
 */
const removeNoneDataChildren = (node, childKey = 'children') => {
    const remove = nodeItem => {
        if (nodeItem[childKey] && nodeItem[childKey].length === 0) {
            delete nodeItem[childKey];
        } else if (nodeItem[childKey]) {
            nodeItem[childKey].forEach(remove);
        }

    }
    node.forEach(remove);
    return node
}

/**
 * 扁平数据转树结构
 * @param {[]} flatList 扁平化树的数据
 * @param {String} [idKey="id"] 主字段 默认-id
 * @param {String} [parentKey="parentId"] 父级字段 默认-parentId
 * @param {string} [childKey='children'] 子级字段 默认-children
 * @param {boolean} [delNoneDataChildren=true] 删除子级数据为空的子级 默认-true
 * @returns {[]}
 */
const buildTree = (flatList, idKey = 'id', parentKey = 'parentId', childKey = 'children', delNoneDataChildren = true) => {
    const map = {};
    const roots = [];

    flatList.forEach(item => {
        map[item[idKey]] = { ...item, [childKey]: [] };
    });

    flatList.forEach(item => {
        const itemId = item[idKey];
        const parentId = item[parentKey];

        if (parentId !== null && map[parentId]) {
            map[parentId][childKey].push(map[itemId]);
        } else {
            roots.push(map[itemId]);
        }
    });

    if (delNoneDataChildren) return removeNoneDataChildren(roots, childKey);
    return roots;
}


// 使用
const test =[{label:'疼逊有限公司',code:'1212',disabled:false,parentId:'none'},{label:'财务部',code:'34343',disabled:false,parentId:'1212'},{label:'人事部',code:'43434',disabled:false,parentId:'1212'},{label:'经营部',code:'53543',disabled:false,parentId:'1212'},{label:'市场部',code:'23232',disabled:true,parentId:'53543'},{label:'销售部',code:'3435435',disabled:false,parentId:'53543'}];
// 使用自定义字段
const tree = buildTree(test, 'code', 'parentId', 'children');
console.log("1 ~ tree:", JSON.stringify(tree))
相关推荐
黄毛火烧雪下几秒前
Java 基础笔记:文件、递归与字符编码
java·开发语言·笔记
学计算机的计算基2 分钟前
链表算法上篇:LeetCode 206/234/141/142/160/21 题解与易错点
java·笔记·算法·链表
丷丩4 分钟前
MapLibre GL JS第50课:用表达式创建虚线渐变线
javascript·gis·mapbox·maplibre gl js
信也科技布道师4 分钟前
从Istio 503 NC 错误深入理解 Mesh 路由全链路原理
java·服务器·网络
swordbob8 分钟前
3 大 I/O 模型BIO / NIO / AIO
java·linux·spring
Pluto_CSND10 分钟前
Cron表达式使用说明
java
十五喵源码网10 分钟前
基于SpringBoot2+vue2的酒店客房管理系统
java·毕业设计·springboot·论文笔记
疯狂成瘾者24 分钟前
Java 常用工具包 java.util
java·开发语言·windows
ywl47081208729 分钟前
springSecurity+jwt,简单版demo
java·前端·servlet
SXJR1 小时前
spring boot + langchain4j +milvus实现向量存储
java·spring boot·后端·大模型·milvus·rag·langchain4j