如何递归树并创建父路径节点数组

How to recurse a tree and create an array of parent path nodes?

本文关键字:创建 路径 节点 数组 何递归 递归      更新时间:2023-09-26

>我有一棵树,看起来像这样:

{
    "name": "A",
    "children": [
        {
            "name": "B",
            "children": [
                {
                    "name": "C",
                    "children": [
                        {
                            "name": "D"
                        },
                        {
                            "name": "E"
                        }
                    ]
                },
                {
                    "name": "F"
                }
            ]
        },
        {
            "name": "G",
            "children": [
                {
                    "name": "H"
                }
            ]
        }
    ]
}

我想遍历我的树,并为每个节点添加父组件的路径信息。因此,使用我上面的例子,我正在寻找如何生成它:

{
    "name": "A",
    "path": [
        "A"
    ],
    "children": [
        {
            "name": "B",
            "path": [
                "A",
                "B"
            ],
            "children": [
                {
                    "name": "C",
                    "path": [
                        "A",
                        "B",
                        "C"
                    ],
                    "children": [
                        {
                            "name": "D",
                            "path": [
                                "A",
                                "B",
                                "C",
                                "D"
                            ]
                        },
                        {
                            "name": "E",
                            "path": [
                                "A",
                                "B",
                                "C",
                                "E"
                            ]
                        }
                    ]
                },
                {
                    "name": "F",
                    "path": [
                        "A",
                        "B",
                        "F"
                    ]
                }
            ]
        },
        {
            "name": "G",
            "path": [
                "A",
                "G"
            ],
            "children": [
                {
                    "name": "H",
                    "path": [
                        "A",
                        "G",
                        "H"
                    ]
                }
            ]
        }
    ]
}

这是设置利用this的路径的小递归迭代建议。

源:

  • Function#bind()

The bind()方法创建一个新函数,该函数在调用时将其 this 关键字设置为提供的值,并在调用新函数时提供的任何参数序列之前具有给定的参数序列。

  • Array#forEach(),这个参数:

如果向 forEach() 提供了 thisArg 参数,则在调用时将传递给回调,以用作其 this 值。否则,将传递值undefined以用作其此值。最终可由回调观察到的 this 值是根据确定函数看到的 this 的常规规则确定的

function setPath(o) {
    o.path = this.concat(o.name);
    Array.isArray(o.children) && o.children.forEach(setPath, o.path);
};
var object = { "name": "A", "children": [{ "name": "B", "children": [{ "name": "C", "children": [{ "name": "D" }, { "name": "E" }] }, { "name": "F" }] }, { "name": "G", "children": [{ "name": "H" }] }] };
setPath.bind([])(object);
document.write('<pre>' + JSON.stringify(object, 0, 4) + '</pre>');