javascript - 怎么递归输出这个对象?

 长发及腰和我娶你D有毛关系 发布于 2022-11-24 13:50
var orz = {
    it: {
        facebook: {
            apple: {
                google: {
                    twitter: {}
                },
                microsoft: {}
            }
        }
    },
    china: {}
};

function objLength(obj) {
    var j = 0;
    for (i in obj) {
        j++;
    }
    return j;
}

function re(ja, num, tree, jn) {
    if (objLength(ja) == 0) {
        return;
    }
    if ("undefined" == typeof num) {
        num = 0;
    }
    if ("undefined" == typeof tree) {
        tree = [];
    }
    if ("undefined" != typeof jn) {
        tree.push(jn);
    }
    num++;
    fo(ja, tree, num);

    function fo(a, b, c) {
        for (x in a) {
            if (b.length > 0) {
                console.log(c + ": " + b + "," + x);
            } else {
                console.log(c + ": " + x);
            }
            re(a[x], c, b, x);
        }
    }
}

re(orz);

/**
 * 为什么结果是:
 * 1: it
 * 2: it,facebook
 * 3: it,facebook,apple
 * 4: it,facebook,apple,google
 * 5: it,facebook,apple,google,twitter
 * 4: it,facebook,apple,google,microsoft
 * 1: it,facebook,apple,google,china
 * 而不是
 * 1: it
 * 2: it,facebook
 * 3: it,facebook,apple
 * 4: it,facebook,apple,google
 * 5: it,facebook,apple,google,twitter
 * 4: it,facebook,apple,microsoft
 * 1: china
 *
 */
修改
3 个回答
  • var orz = {
        it: {
            facebook: {
                apple: {
                    google: {
                        twitter: {}
                    },
                    microsoft: {}
                }
            }
        },
        china: {}
    };
    
    var re=function(current,path){
        for(var c in current){
            path.push(c);
            console.log(path.length + ":"+ path.join(","));
            re(current[c],path);
            path.pop();
        }
    }
    
    re(orz,[]);
    
    2022-11-24 14:54 回答
  • 因为你用了push,所以当循环遍历到google时,tree为["it", "facebook", "apple", "google"],接下来console输出的三个记录就是:
    5: it,facebook,apple,google,twitter
    4: it,facebook,apple,google,microsoft
    1: it,facebook,apple,google,china
    解决方法有两个:
    1.push后进行pop操作

    num++;
    fo(ja, tree, num);
    tree.pop(jn);
    

    2.使用String代替Array

    tree += jn;
    
    2022-11-24 14:54 回答
  • var orz = {
        it: {
            facebook: {
                apple: {
                    google: {
                        twitter: {}
                    },
                    microsoft: {}
                }
            }
        },
        china: {}
    };
    
    var result = [];
    
    function re(obj, prev) {
        Object.keys(obj).forEach(function(item) {
            var hehe = '';
            if (obj[item] instanceof Object && !(obj[item] instanceof Array)) {
            //如果数组也算,使用下边的条件
            //if (obj[item] instanceof Object) {
                hehe = (prev.length ? prev + ',' + item : item);
                result.push(hehe);
                re(obj[item], hehe);
            }
        });
    }
    re(orz, '');
    console.log(result);
    
    2022-11-24 14:54 回答
撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有