javascript 如何获取未知 JSON 层次结构的总深度?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16075664/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 03:11:27  来源:igfitidea点击:

How to get the total depth of an unknown JSON hierarchy?

javascriptjsond3.jsnested

提问by jfk83

I've been struggling to find/build a recursive function to parse this JSON file and get the total depth of its children.

我一直在努力寻找/构建一个递归函数来解析这个 JSON 文件并获取其子项的总深度。

The file looks something like this:

该文件如下所示:

var input = {
    "name": "positive",
    "children": [{
        "name": "product service",
        "children": [{
            "name": "price",
            "children": [{
                "name": "cost",
                "size": 8
            }]
        }, {
            "name": "quality",
            "children": [{
                "name": "messaging",
                "size": 4
            }]
        }]
    }, {
        "name": "customer service",
        "children": [{
            "name": "Personnel",
            "children": [{
                "name": "CEO",
                "size": 7
            }]
        }]
    }, {
        "name": "product",
        "children": [{
            "name": "Apple",
            "children": [{
                "name": "iPhone 4",
                "size": 10
            }]
        }]
    }] 
}

回答by Christopher Chiche

You can use a recursive function to go through the whole tree:

您可以使用递归函数遍历整个树:

getDepth = function (obj) {
    var depth = 0;
    if (obj.children) {
        obj.children.forEach(function (d) {
            var tmpDepth = getDepth(d)
            if (tmpDepth > depth) {
                depth = tmpDepth
            }
        })
    }
    return 1 + depth
}

The function works as follow:

该函数的工作原理如下:

  • If the object is not a leaf (i.e the object has the children attribute), then:
    • Compute the depth of each child, save the maximal one
    • return 1 + the depth of the deepest child
  • Otherwise, return 1
  • 如果对象不是叶子(即对象具有 children 属性),则:
    • 计算每个孩子的深度,保存最大的一个
    • return 1 + 最深孩子的深度
  • 否则返回 1

jsFiddle: http://jsfiddle.net/chrisJamesC/hFTN8/

jsFiddle:http: //jsfiddle.net/chrisJamesC/hFTN8/

EDITWith modern JavaScript, the function could look like this:

编辑使用现代 JavaScript,该函数可能如下所示:

const getDepth = ({ children }) => 1 +
    (children ? Math.max(...children.map(getDepth)) : 0)

jsFiddle: http://jsfiddle.net/chrisJamesC/hFTN8/59/

jsFiddle:http: //jsfiddle.net/chrisJamesC/hFTN8/59/

回答by minikomi

This will count the number of "leaves" in a tree:

这将计算树中“叶子”的数量:

var treeCount = function (branch) {
    if (!branch.children) {
        return 1;
    }
    return branch.children.reduce(function (c, b) {
        return c + treeCount(b);
    }, 0)
}

And an alternative way to get depth:

以及获得深度的另一种方法:

var depthCount = function (branch) {
    if (!branch.children) {
        return 1;
    }
    return 1 + d3.max(branch.children.map(depthCount));
 }