zoukankan      html  css  js  c++  java
  • js获取多层json最深层级数

    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);

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

  • 相关阅读:
    Ajax
    Linux安装SmartSVN及破解
    JQuery异步提交
    动画效果
    事件
    表单选择器
    DOM操作
    JQuery基础
    PHP环境配置
    DP--钢条切割
  • 原文地址:https://www.cnblogs.com/hanlei525/p/9613004.html
Copyright © 2011-2022 走看看