js获取多层json最深层级数

版权声明:博客内容属于本人原创,转载请注明出处 https://blog.csdn.net/hl_qianduan/article/details/81988180
var data = {
    "list": [{
        "list": [ {
            "list": [{
                "list": [{"list": [{"four": "4", "one": "1", "three": "3", "two": "3"}], "value": "4"}],
                "value": "3"
            }, {
                "list": [{
                    "list": [{"four": "4", "one": "1", "three": "4", "two": "3"}, {
                        "four": "4",
                        "one": "1",
                        "three": "4",
                        "two": "3"
                    }], "value": "4"
                }], "value": "4"
            }], "value": "3"
        }], "value": "1"
    }, {
        "list": [{
            "list": [{
                "list": [{"list": [{"four": "4", "one": "2", "three": "3", "two": "2"}], "value": "4"}],
                "value": "3"
            }], "value": "2"
        }], "value": "2"
    }]
}
var num = 0;
function recursion(obj,k) {
    num = Math.max(num,k);
    console.log(k);
    if (obj.list)
        obj.list.forEach(function(v, i){
            recursion(v,k+1);
        });
}
recursion(data,1);
console.log("最深层级数"+num);

我遇到的问题,得到大神解决,特此分享

猜你喜欢

转载自blog.csdn.net/hl_qianduan/article/details/81988180
今日推荐