js将扁平数组转化为树形数组

let list = [
            { id: '1', content: '河北省' },
            { id: '2', content: '山东省' },
            { id: '3', content: '河南省' },
            { id: '1-1', pid: '1', content: '廊坊市' },
            { id: '1-1-1', pid: '1-1', content: '安次区' },
            { id: '1-1-1-1', pid: '1-1-1', content: '新中街' },
            { id: '1-1-1-2', pid: '1-1-1', content: '育才路' },
            { id: '1-1-2', pid: '1-1', content: '广阳区' },
            { id: '1-2', pid: '1', content: '石家庄市' },
            { id: '1-3', pid: '1', content: '唐山市' },
            { id: '2-1', pid: '2', content: '济南市' },
            { id: '2-2', pid: '2', content: '青岛市' },
            { id: '2-3', pid: '2', content: '潍坊市' },
        ];
        //将以上扁平数组改为以下树形数组
        // let result = [
        //     {
        //         id: '1',
        //         content: '河北省',
        //         children: [
        //             {
        //                 id: '1-1',
        //                 pid: '1',
        //                 content: '廊坊市',
        //                 children: [
        //                     {
        //                         id: '1-1-1',
        //                         pid: '1-1',
        //                         content: '安次区',
        //                         children: [
        //                             {
        //                                 id: '1-1-1-1',
        //                                 pid: '1-1-1',
        //                                 content: '新中街'
        //                             },
        //                             {
        //                                 id: '1-1-1-2',
        //                                 pid: '1-1-1',
        //                                 content: '育才路'
        //                             }
        //                         ]
        //                     },
        //                     {
        //                         id: '1-1-2',
        //                         pid: '1-1',
        //                         content: '广阳区'
        //                     }
        //                 ]
        //             },
        //             {
        //                 id: '1-2',
        //                 pid: '1',
        //                 content: '石家庄市'
        //             },
        //             {
        //                 id: '1-3',
        //                 pid: '1',
        //                 content: '唐山市'
        //             }
        //         ]
        //     },
        //     {
        //         id: '2',
        //         content: '山东省',
        //         children: [
        //             {
        //                 id: '2-1',
        //                 pid: '2',
        //                 content: '济南市'
        //             },
        //             {
        //                 id: '2-2',
        //                 pid: '2',
        //                 content: '青岛市'
        //             },
        //             {
        //                 id: '2-3',
        //                 pid: '2',
        //                 content: '潍坊市'
        //             }
        //         ]
        //     },
        //     {
        //         id: '3',
        //         content: '河南省'
        //     }
        // ];
        //给list中每个元素增加一个children属性,并将list中的
        //元素重新排序,将子节点放到对应的父节点的children中
        function makeTree(list) {
            let temp = new Map();
            let result = [];
            //遍历list中的元素,将list转换成一个map,其中key为id,
            //value为元素对象
            list.forEach(element => {
                temp.set(element.id, element);
            });
            console.log(temp)
            //重新排序list,将每个元素插入到对应父节点的children属性中
            list.forEach(element => {
                if (element.pid) {
                    if (temp.get(element.pid).children == undefined) {
                        temp.get(element.pid).children = []
                        temp.get(element.pid).children.push(element)
                    } else {
                        temp.get(element.pid).children.push(element)
                    }
                } else {
                    result.push(element)
                }
            })
            console.log(result)
        }
        makeTree(list)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值