Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/algorithm/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 将所有父级的路径分配给;这";树中的节点_Javascript_Algorithm_Tree - Fatal编程技术网

Javascript 将所有父级的路径分配给;这";树中的节点

Javascript 将所有父级的路径分配给;这";树中的节点,javascript,algorithm,tree,Javascript,Algorithm,Tree,对于给定的输入 [ {id:1,路径:'foo/bar/1'}, {id:2,路径:'foo/bar/2'}, ] 我生成一棵树,如下所示,它是由/分割的每个路径的树表示 { “路径”:“foo”, “儿童”:[ { “路径”:“条”, “儿童”:[ { “路径”:“1”, “儿童”:[…], “id”:1, “嵌套级别”:2 }, { “路径”:“2”, “儿童”:[…], “id”:2, “嵌套级别”:2 } ], “嵌套级别”:1 } ], “嵌套级别”:0 } 到目前为止,我有以下代

对于给定的输入

[
{id:1,路径:'foo/bar/1'},
{id:2,路径:'foo/bar/2'},
]
我生成一棵树,如下所示,它是由
/
分割的每个路径的树表示

{
“路径”:“foo”,
“儿童”:[
{
“路径”:“条”,
“儿童”:[
{
“路径”:“1”,
“儿童”:[…],
“id”:1,
“嵌套级别”:2
},
{
“路径”:“2”,
“儿童”:[…],
“id”:2,
“嵌套级别”:2
}
],
“嵌套级别”:1
}
],
“嵌套级别”:0
}
到目前为止,我有以下代码实现上述功能:

常量输入数组=[
{id:1,路径:'foo/bar/1'},
{id:2,路径:'foo/bar/2'},
];
常量结果=[];
常量级别={result};
//出树
for(输入阵列的p){
让我们坚持下去;
设{path,…rest}=p;
path.split('/').reduce((r,path,i,{length})=>{
如果(!r[path]){
r[path]={result:[]};
last={path,children:r[path].result};
r、 结果:推(最后一次);
}
返回r[path];
},水平);
对象。分配(最后,剩余);
}
//遍历树并指定嵌套级别(用于UI内容)
函数遍历(obj,嵌套级别=0){
if(obj!==null&&typeof obj==object){
Object.entries(obj.forEach)([key,value])=>{
value.nestingLevel=nestingLevel;
横穿(
价值
//如果我们要检查孩子,不要增加水平[]
typeof value=='object'&&!Array.isArray(值)?nestingLevel+1:nestingLevel
);
});
}
}
导线测量(结果);
log(JSON.stringify(结果[0],null,4));
我想做的(但还不知道如何做)是将完整的父路径(如
fullPath
)添加到每个节点,以便输出如下所示:

{
“路径”:“foo”,
“完整路径”:“foo”,
“儿童”:[
{
“路径”:“条”,
“完整路径”:“foo/bar”,
“儿童”:[
{
“路径”:“1”,
“完整路径”:“foo/bar/1”,
“儿童”:[…],
“id”:1,
“嵌套级别”:2
},
{
“路径”:“2”,
“完整路径”:“foo/bar/2”,
“儿童”:[…],
“id”:2,
“嵌套级别”:2
}
],
“嵌套级别”:1
}
],
“嵌套级别”:0
}

有人愿意分享他们的想法吗?

您可以扩展“traverse()”函数,并添加累积路径。每次你下台时,再加一个角色。在每个对象层级中指定

您的遍历函数可以这样更改:

// Walk the tree and assign nesting level (intended for UI stuff)
function traverse(obj, nestingLevel = 0,fullp = "")
{
  if (obj !== null && typeof obj == 'object')
  {
    Object.entries(obj).forEach(([key, value]) => {
      value.nestingLevel = nestingLevel;
      if(typeof value == 'object' && !Array.isArray(value))
        traverse(value,nestingLevel+1,fullp);
      else{
        fp = fullp + "/" + obj.path;
        obj.fullPath = fp;
        traverse(value,nestingLevel,fp);
      }
    });
  }
}
结果是:

{
    "path": "foo",
    "children": [
        {
            "path": "bar",
            "children": [
                {
                    "path": "1",
                    "children": [],
                    "id": 1,
                    "nestingLevel": 2,
                    "fullPath": "/foo/bar/1"
                },
                {
                    "path": "2",
                    "children": [],
                    "id": 2,
                    "nestingLevel": 2,
                    "fullPath": "/foo/bar/2"
                }
            ],
            "nestingLevel": 1,
            "fullPath": "/foo/bar"
        }
    ],
    "nestingLevel": 0,
    "fullPath": "/foo"
}

您可以扩展“traverse()”函数,并添加累积路径。每次你下台时,再加一个角色。在每个对象层级中指定

您的遍历函数可以这样更改:

// Walk the tree and assign nesting level (intended for UI stuff)
function traverse(obj, nestingLevel = 0,fullp = "")
{
  if (obj !== null && typeof obj == 'object')
  {
    Object.entries(obj).forEach(([key, value]) => {
      value.nestingLevel = nestingLevel;
      if(typeof value == 'object' && !Array.isArray(value))
        traverse(value,nestingLevel+1,fullp);
      else{
        fp = fullp + "/" + obj.path;
        obj.fullPath = fp;
        traverse(value,nestingLevel,fp);
      }
    });
  }
}
结果是:

{
    "path": "foo",
    "children": [
        {
            "path": "bar",
            "children": [
                {
                    "path": "1",
                    "children": [],
                    "id": 1,
                    "nestingLevel": 2,
                    "fullPath": "/foo/bar/1"
                },
                {
                    "path": "2",
                    "children": [],
                    "id": 2,
                    "nestingLevel": 2,
                    "fullPath": "/foo/bar/2"
                }
            ],
            "nestingLevel": 1,
            "fullPath": "/foo/bar"
        }
    ],
    "nestingLevel": 0,
    "fullPath": "/foo"
}

考虑使用Loalas.js,忘掉痛苦!考虑使用Loalas.js,忘掉痛苦!