Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/15.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
分层JSON-查找对象的完整路径_Json_Typescript_Hierarchical Data - Fatal编程技术网

分层JSON-查找对象的完整路径

分层JSON-查找对象的完整路径,json,typescript,hierarchical-data,Json,Typescript,Hierarchical Data,我有一个很大的JSON对象,用于树源代码。在这个JSON中,我需要找到一个具有给定ID的对象,并为它构建完整的代码,这意味着收集它的父对象。举例来说,它类似于文件系统:folde1/folder2/folder3/file.txt。 下面是查找对象的递归函数: private find(source, id): string[] { for (const key in source) { var item = source[key]; if (it

我有一个很大的JSON对象,用于树源代码。在这个JSON中,我需要找到一个具有给定ID的对象,并为它构建完整的代码,这意味着收集它的父对象。举例来说,它类似于文件系统:folde1/folder2/folder3/file.txt。 下面是查找对象的递归函数:

private find(source, id): string[] {
    for (const key in source)
    {
        var item = source[key];
        if (item.id === id) {
            return this.indexes;
        }                
        
        if (item.children) {
            this.indexes.push(key);
            var subresult = this.find(item.children, id);
            if (subresult) {
                this.indexes.push(key);
                return this.indexes;
            }                    
        }
    }
    return null;
}
我的问题是,在寻找对象的过程中,我发现了很多虚假的父母,因此结果有额外的数据

你知道怎么做吗?我也在考虑从内部开始寻找路径,但不知道如何找到找到的对象的父对象


谢谢你的帮助。

我将给出一个一般性的答案,希望它适用于你的实际结构。我假设
如下所示:

interface Tree {
  id: string;
  children?: Record<string, Tree>;
}
这里的方法是,函数采用与当前树节点路径相对应的参数,后续路径条目可以附加到该参数。如果忽略此路径,则假定它为空。如果传入的
具有所需的
id
,那么我们已经找到了它,可以返回
curPath
。否则,我们将开始检查
子对象的每个属性(如果存在),查看id是否存在于其中一个子对象中,调用子对象值上的
findPathId
,并通过将子对象键附加到当前路径上,生成一个新的
curPath
。如果我们得到一个结果,我们将返回它。否则,我们继续下一个孩子。如果我们在检查当前项并递归向下遍历其子项后没有找到id,那么它就不在其中,我们返回
未定义的


这里有一个测试:

const tree: Tree = {
  id: "A",
  children: {
    x: {
      id: "B", children: {
        t: { id: "E" },
        u: { id: "F" }
      }
    },
    y: {
      id: "C", children: {
        v: { id: "G" },
        w: { id: "H" }
      }
    },
    z: { id: "D" }
  }
}

console.log(findPathById(tree, "A")); // []
console.log(findPathById(tree, "D")); // ["z"]
console.log(findPathById(tree, "G")); // ["y", "v"]
console.log(findPathById(tree, "J")); // undefined
这看起来像是我们想要的行为。
“A”
id位于根目录下,因此路径为空数组。
“D”
id位于
树.children.z
,因此路径是
[“z”]
。在
树.children.y.children.v
中可以找到
“G”
id,因此路径是
[“y”,“v”]
。最后,没有找到
“J”
id,因此路径是
未定义的



“hieratic”实际上是一个词,所以我不打算更改它,但我想你的意思是“Hieratical”:。哦,孩子,谢谢你!第二语言问题:(
const tree: Tree = {
  id: "A",
  children: {
    x: {
      id: "B", children: {
        t: { id: "E" },
        u: { id: "F" }
      }
    },
    y: {
      id: "C", children: {
        v: { id: "G" },
        w: { id: "H" }
      }
    },
    z: { id: "D" }
  }
}

console.log(findPathById(tree, "A")); // []
console.log(findPathById(tree, "D")); // ["z"]
console.log(findPathById(tree, "G")); // ["y", "v"]
console.log(findPathById(tree, "J")); // undefined