javascript - 层次树中的递归

标签 javascript recursion tree hierarchy

我正在尝试遍历 javascript 中的分层树来确定它有多少层。这是我的树的一小段:

parent: [
   { id: 1 }
   {
       child1: [
           { id: 2 }
           {
               child2: [
                   { id: 3 }
                   {}
               ]
           }
       ],
       child3: [
           { id: 4 }
           { 
               child4: [
                   { id: 5 }
                   {}
               ],
               child5: [
                   { id: 6 }
                   {
                       child6: [
                           { id: 7 }
                           {}
                       ]
                   }
               ]
           }
       ]
   }
]

将会有数量未知的 parent 和 child 。有 1 个确定性:

  • 每个元素(例如父元素)的数组中始终有 2 个对象。 第一个对象始终是一个 ID。 第二个对象包含它所拥有的子对象。这可能是空的或已填充

我的目标是确定树的层数。例如,此示例树中有 4 个级别(parent = 1、child1 + child3 位于同一级别 (2)、child4 和 child5 位于同一级别 (3)、child6 = 4)。

这是我到目前为止的代码:

for (var j in dependencyTree) {
    if (getObjectSize(dependencyTree[j][1]) > 0) {
        levelsArray.push(j + ': ' + recursiveFunction(dependencyTree[j][1], 1));
    }
}


function recursiveFunction(obj, lvls) {
    if (getObjectSize(obj) > 0) {
        for (var i in obj) {
            recursiveFunction(obj[i][1], lvls++);
        }
    }
    return lvls;
}

getObjectSize() 仅返回对象的大小。 IE。它有多少个直系 child 。例如,对象 parent 将返回 2(child1child3)。

一开始,顶级父级子级被传递到函数中。

我认为我的问题是 for 循环 (for (var i in obj)) 因为这可能会捕获第一个子 parent 有(child1),最终将返回 child1 具有的级别数,即使 child3 有更多级别。

感谢任何帮助。

(尚未尝试 lodash,但被告知它不提供递归帮助)

编辑

{
    "Mobile": [
        {
            "id": 89
        },
        { 
            "Mobile Client": [
                {
                    "id": 100 
                },
                {}
            ]
        }
    ],
    "Service Platform": [
        {
            "id": 90
        },
        {
            "Service Platform": [
                {..."

编辑(新建议格式):

我和我的同事讨论过,新提议的数据格式是:

[
    {
        "name": "Mobile",
        "id": 89,
        "children": [
            {
                "name": "Mobile Client",
                "id": 100,
                "children": {}
            }
        ]
    }
];

这似乎是更可行的数据,并将于明天实现

最佳答案

无论格式如何,该解决方案都会迭代数组和对象中的所有元素并对它们进行计数。

function count(array) {
    var c = 0;
    array.forEach(function (a) {
        c++;
        if (typeof a === 'object') {
            Object.keys(a).forEach(function (k) {
                if (Array.isArray(a[k])) {
                    c += count(a[k]);
                }
            });
        }
    });
    return c;
}

var parent = [{ id: 1 }, { child1: [{ id: 2 }, { child2: [{ id: 3 }, {}, ] }], child3: [{ id: 4 }, { child4: [{ id: 5 }, {}], child5: [{ id: 6 }, { child6: [{ id: 7 }, {}] }] }] }],
    newFormat = [{ "name": "Mobile", "id": 89, "children": [{ "name": "Mobile Client", "id": 100, "children": {} }] }];

document.write('<pre>' + JSON.stringify(count(parent), 0, 4) + '</pre>');
document.write('<pre>' + JSON.stringify(parent, 0, 4) + '</pre><hr>');
document.write('<pre>' + JSON.stringify(count(newFormat), 0, 4) + '</pre>');
document.write('<pre>' + JSON.stringify(newFormat, 0, 4) + '</pre>');

关于javascript - 层次树中的递归,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35921853/

相关文章:

javascript - 允许从 Express 中的不同文件访问变量

java - 我的项目需要一点帮助

c - 实现一个函数以递归方式返回有效的获胜条件 - C

java - 用Java构建一棵树

algorithm - 从二叉树中删除重复项

c++ - 解析文本以创建树状数据结构

javascript - 如何使用foreach循环在mysql表中插入一个静态值和多个动态值?

javascript - 如果源为空,如何将 id 为 false 的 img 标签设置为 false?

javascript - 使用 jquery 从左向右滑动

c - 在C程序中使用递归来解决序列中的位置索引?