Javascript树型数据扁平化与扁平化数组转树型结构数据

前言

在前端开发中,我们经常会遇到树型结构的数据,也可能会遇到已经被扁平化了的数据,为了更方便的操作这些数据
我们就需要两个方法“扁平化数据”、“扁平化数据转树型数据”。

测试树型数据

const dataList = [
    {
        id: 1,
        name: '菜单A',
        children: [
            {
                id: 5,
                parentId: 1,
                name: '菜单A - 1'
            },
            {
                id: 6,
                parentId: 1,
                name: '菜单A - 2',
                children: [
                    {
                        id: 10,
                        parentId: 6,
                        name: '菜单A - 2 - 1'
                    }
                ]
            }
        ]
    },
    {
        id: 2,
        name: '菜单B',
        children: [
            {
                id: 7,
                parentId: 2,
                name: '菜单B - 1',
                children: [
                    {
                        id: 11,
                        parentId: 7,
                        name: '菜单B - 1 - 1'
                    }
                ]
            }
        ]
    },
    {
        id: 3,
        name: '菜单C',
        children: [
            {
                id: 8,
                parentId: 3,
                name: '菜单C - 1'
            }
        ]
    },
    {
        id: 4,
        name: '菜单D',
        children: [
            {
                id: 9,
                parentId: 4,
                name: '菜单D - 1'
            }
        ]
    }
]

扁平化数组方法

// 判断数组是否可用
const isAvailableArray = (arr) => Array.isArray(arr) && arr.length > 0;

function flatTree(array, parentId) {
    return array.reduce((tree, cur) => {
        // 拿到遍历数据的 children
        const children = cur.children
        // return 返回新的数组
        return [
            ...tree, // 解构 tree
            parentId ? { ...cur, parentId } : cur,
            // 判断当前的children是否可用,可用时直接继续调用flatTree方法,否则就是空数组
            ...(isAvailableArray(children) ? flatTree(children, cur.id) : [])
        ]
    }, [])
}

console.log(flatTree(dataList))

输出结果以下
在这里插入图片描述

扁平化数组转树型结构

const flatArray = [
    {
        "id": 1,
        "name": "菜单A",
    },
    {
        "id": 5,
        "parentId": 1,
        "name": "菜单A - 1"
    },
    {
        "id": 6,
        "parentId": 1,
        "name": "菜单A - 2",
    },
    {
        "id": 10,
        "parentId": 6,
        "name": "菜单A - 2 - 1"
    },
    {
        "id": 2,
        "name": "菜单B",
    },
    {
        "id": 7,
        "parentId": 2,
        "name": "菜单B - 1",
    },
    {
        "id": 11,
        "parentId": 7,
        "name": "菜单B - 1 - 1"
    },
    {
        "id": 3,
        "name": "菜单C",
    },
    {
        "id": 8,
        "parentId": 3,
        "name": "菜单C - 1"
    },
    {
        "id": 4,
        "name": "菜单D",
    },
    {
        "id": 9,
        "parentId": 4,
        "name": "菜单D - 1"
    }
]

function convertTree(flatArrayData, parentId, list = []) {
    flatArrayData.forEach((item) => {
        item.children = flatArrayData.filter((i) => i.parentId == item.id)
        if (item.parentId == parentId) {
            list.push(item)
        }
    })
    return list
}

console.log(convertTree(flatArray))

输出结果以下
在这里插入图片描述

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值