Javascript 从json获取分层数据

Javascript 从json获取分层数据,javascript,json,algorithm,Javascript,Json,Algorithm,我想写一个函数,它接受一个键并递归地获取它的所有子项 var oTeamHierarchyJson = [ { "Key": "10011", "Name": "A", "Job Title": "VP", "children": "C", "Pare

我想写一个函数,它接受一个键并递归地获取它的所有子项

var oTeamHierarchyJson = [
            {
                "Key": "10011",
                "Name": "A",                
                "Job Title": "VP",                
                "children": "C",
                "Parent": "1000",
                "Level": "1"
            },
            {
                "Key": "10012",
                "Name": "B",                
                "Job Title": "VP",                
                "children": "D",
                "Parent": "1001",
                "Level": "1"
            },
            {
                "Key": "10013",
                "Name": "C",               
                "Job Title": "GM",                
                "children": "E",
                "Parent": "10011",
                "Level": "2"
            },
            {
                "Key": "10014",
                "Name": "D",
                "Job Title": "MP",                
                "children": "F",
                "Parent": "10013",
                "Level": "3"
            }
        ];
function filterJSONData(currentKey) {
      //return all children recursively
}

对于10011-return 10013、10014

数据结构只保存父关系,而不保存子关系,这实际上不适合遍历。有两种方法可以避免这一缺点:

  • 处理数组,使每个注释都引用其所有子项
  • 引入一个helper函数,该函数返回节点的所有子节点

  • 之后,递归地查找给定节点的所有后续节点就容易多了。

    数据结构只保存父关系,而不保存子关系,这实际上不适合遍历。有两种方法可以避免这一缺点:

    var oTeamHierarchyJson = [
                {
                    "Key": "10011",
                    "Name": "A",                
                    "Job Title": "VP",                
                    "children": "C",
                    "Parent": "1000",
                    "Level": "1"
                },
                {
                    "Key": "10012",
                    "Name": "B",                
                    "Job Title": "VP",                
                    "children": "D",
                    "Parent": "1001",
                    "Level": "1"
                },
                {
                    "Key": "10013",
                    "Name": "C",               
                    "Job Title": "GM",                
                    "children": "E",
                    "Parent": "10011",
                    "Level": "2"
                },
                {
                    "Key": "10014",
                    "Name": "D",
                    "Job Title": "MP",                
                    "children": "F",
                    "Parent": "10013",
                    "Level": "3"
                }
            ];
    
    function filterJSONData(currentKey) {
          //return all children recursively
    }
    
  • 处理数组,使每个注释都引用其所有子项
  • 引入一个helper函数,该函数返回节点的所有子节点

  • 之后,递归地查找给定节点的所有后续节点就容易多了。

    您可以从平面数据列表构建树。下面的代码递归地添加一个属性子节点,并用每个节点的子节点填充它。
    var oTeamHierarchyJson = [
                {
                    "Key": "10011",
                    "Name": "A",                
                    "Job Title": "VP",                
                    "children": "C",
                    "Parent": "1000",
                    "Level": "1"
                },
                {
                    "Key": "10012",
                    "Name": "B",                
                    "Job Title": "VP",                
                    "children": "D",
                    "Parent": "1001",
                    "Level": "1"
                },
                {
                    "Key": "10013",
                    "Name": "C",               
                    "Job Title": "GM",                
                    "children": "E",
                    "Parent": "10011",
                    "Level": "2"
                },
                {
                    "Key": "10014",
                    "Name": "D",
                    "Job Title": "MP",                
                    "children": "F",
                    "Parent": "10013",
                    "Level": "3"
                }
            ];
    
    function filterJSONData(currentKey) {
          //return all children recursively
    }
    

    buildTree=函数(数组,节点){
    //获取节点
    var节点=null;
    
    对于(var i=0;i,您可以从平面数据列表构建一棵树。 看

    buildTree=函数(数组,节点){
    //获取节点
    var节点=null;
    
    对于(var i=0;iSo)您的函数进展如何?可能希望将其添加到问题中?那么您的函数进展如何?可能希望将其添加到问题中?在JSON中添加子项在JSON中添加子项