首页 > 其他 > 详细

递归 判断条件 赋值

时间:2021-05-25 23:46:45      阅读:34      评论:0      收藏:0      [点我收藏+]
 // 递归 判断条件 赋值
        var data = [
            {
                "addr": "00001",
                "parent": "00002",
                "num": "00002",
            },
            {
                "addr": "00002",
                "parent": "00002",
                "num": "00002",
            },
            {
                "addr": "00002",
                "parent": "00002",
                "num": "00002",
            },
            {
                "addr": "00003",
                "parent": "00003",
                "num": "00002",
            },
            {
                "addr": "00004",
                "parent": "00003",
                "num": "00002",
            },
            {
                "addr": "00003",
                "parent": "00002",
                "num": "00002",
            }
        ];

        function deepClone(obj) {
            let objClone = Array.isArray(obj) ? [] : {};
            if (obj && typeof obj === "object") {
                for (key in obj) {
                    if (obj.hasOwnProperty(key)) {
                        //判断ojb子元素是否为对象,如果是,递归复制
                        if (obj[key] && typeof obj[key] === "object") {
                            objClone[key] = deepClone(obj[key]);
                        } else {
                            //如果不是,简单复制
                            objClone[key] = obj[key];
                        }
                    }
                }
            }
            return objClone;
        };
        var data2 = deepClone(data);
        console.log(data2);
        for (var i = 0; i < data.length; i++) {
            if (data[i].addr == data[i].parent && data[i].addr == data[i].num) {
                data[i].childer = data2[i];
            }
        }
        console.log(data);

递归 判断条件 赋值

原文:https://www.cnblogs.com/tuziling/p/14810611.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!