流浪のwolf

卷帝

导航

将数组数据转化成树形结构 tranListToTreeData


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);

 

posted on 2022-11-09 01:03  流浪のwolf  阅读(111)  评论(0编辑  收藏  举报