tree树形数据处理 扁平化树状 树状扁平化 处理树里的数据

vue3.0 公司流程图 https://sangtian152.github.io/vue3-tree-org/demo/
vue-tree-color


<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <script>
        // 树的递归 :https://blog.csdn.net/qq_36647492/article/details/121386372
        let arr = [
            {
                parentId: 0,
                id: 235
            },
            {
                parentId: 235,
                id: 337
            },
            {
                parentId: 235,
                id: 329
            },
            {
                parentId: 235,
                id: 344
            },
            {
                parentId: 344,
                id: 615
            },
            {
                parentId: 344,
                id: 614
            },
            {
                parentId: 614,
                id: 615
            }
        ];

        function test(list) {    // 这是个启动方法,需要这个方法发动技能
            let result = [];    //    结果集
            arrayToTree(list, result, 0);    // 递归启动!!!
            return result;
        }

        /**
         * @param {*} data 源数组
         * @param {*} result 将结果添加到这个数组
         * @param {*} pid 父id
         */
        function arrayToTree(data, result, pid) {
            for (let item of data) {    // 每次调用此方法都重新遍历一遍源数组
                if (item.pid === pid) {    // 如果当前的pid等于传进来的父id,则
                    let newItem = { ...item, chilrden: [] };    // 将当前对象所有属性和新增属性chilrden合并成一个新对象
                    result.push(newItem);    // 将新对象添加的传进来的数组中
                    arrayToTree(data, newItem.chilrden, item.id);    // 这里注意,调用递归方法时,将新对象的chilrden作为结果数组传入,下一层的结果则会添加到这一层对象的chilrden中
                }
            }
        }



        // function arrayToTree(list) {
        //     let result = [];    // 结果集
        //     let map = {};

        //     for (let item of list) {    // 遍历一遍源数组
        //         map[item.id] = { ...item, chilrden: [] };    // 将源数组中每一个对象的id作为key,将当前对象所有属性和新增属性chilrden作为value。
        //     }

        //     console.log(map, '===map')

        //     for (let item of list) {
        //         if (item.parentId === 0) {    // 当pid为0时,添加到结果集
        //             let newItem = map[item.id];    // 注意!这里一定要将map[item.id] 赋值给新变量,这样newItem就和map[item.id]指向同一个内存地址了,达到数据共享
        //             result.push(newItem);
        //         } else {
        //             map[item.parentId].chilrden.push(map[item.id]);    // 将key为当前id的对象,添加到key等于pid的对象的chilrden中
        //         }
        //     }
        //     return result;
        // }

        function arrayToTree(list) {
            let result = [];
            let map = {};

            // for(let item of list) {    // 遍历一遍源数组
            //     map[item.id] = {...item, chilrden: []};    // 将源数组中每一个对象的id作为key,将当前对象所有属性和新增属性chilrden作为value。
            // }

            for (let item of list) {

                map[item.id] = { ...item, chilrden: [] };    // 将源数组中每一个对象的id作为key,将当前对象所有属性和新增属性chilrden作为value。

                if (item.parentId === 0) {    // 当pid为0时,添加到结果集
                    let newItem = map[item.id];    // 注意!这里一定要将map[item.id] 赋值给新变量,这样newItem就和map[item.id]指向同一个内存地址了,达到数据共享
                    result.push(newItem);
                } else {
                    map[item.parentId].chilrden.push(map[item.id]);    // 将key为当前id的对象,添加到key等于pid的对象的chilrden中
                }
            }
            return result;
        }


        console.log(arrayToTree(arr), '===parentId')




        let tree = [
            {
                parentId: 320,
                id: 235,
                children: [
                    {
                        parentId: 235,
                        id: 337
                    },
                    {
                        parentId: 235,
                        id: 329
                    },
                    {
                        parentId: 235,
                        id: 344,
                        children: [{
                            parentId: 344,
                            id: 615
                        },
                        {
                            parentId: 344,
                            id: 614
                        }]
                    },
                ]
            },
        ];

        // 树的扁平化
        function readNodes(nodes = [], arr = []) {
            for (let item of nodes) {
                arr.push({
                    parentId: item.parentId,
                    id: item.id
                })
                if (item.children && item.children.length) {
                    readNodes(item.children, arr)
                }
            }
            return arr
        }

        console.log(readNodes(tree), 'readNodes(tree)===')


        // 改变树里面的内容

        let tree1 = [
            {
                parentId: 320,
                id: 235,
                children: [
                    {
                        parentId: 235,
                        id: 337
                    },
                    {
                        parentId: 235,
                        id: 329
                    },
                    {
                        parentId: 235,
                        id: 344,
                        children: [{
                            parentId: 344,
                            id: 615
                        },
                        {
                            parentId: 344,
                            id: 614
                        }]
                    },
                ]
            },
        ];

        function selectChange(arr, result = []) {
            let organizationList = [614]
            let add = (arr, result) => {
                arr.forEach((item) => {
                    item.checked = false;
                    if (organizationList.includes(item.id)) {
                        item.checked = true;
                    }

                    if (item.children && Array.isArray(item.children)) {
                        add(item.children);
                    }
                });
            };
            add(arr, result);
            console.log(tree1, '===tree2')
        }

        selectChange(tree1)

        console.log(tree1, '===tree1')



    </script>

    <script>




    </script>
</body>

</html>
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

web修理工

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值