Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/468.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/14.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 在json树中按id查找节点_Javascript_Json_Javascript Objects - Fatal编程技术网

Javascript 在json树中按id查找节点

Javascript 在json树中按id查找节点,javascript,json,javascript-objects,Javascript,Json,Javascript Objects,我想将子数组添加到节点,其中id=52126f7d(或其他)。我该怎么做 var children = [ { name: 'great-granchild3', id: '2a12a10h' }, { name: 'great-granchild4', id: 'bpme7qw0' } ] // json tree var objects = { name: 'all objects', id:"2e6ca1c3", children:

我想将
子数组添加到节点,其中
id=52126f7d
(或其他)。我该怎么做

var children = [
  { name: 'great-granchild3',
    id: '2a12a10h'
  },
  { name: 'great-granchild4',
    id: 'bpme7qw0'
  }
]

// json tree
var objects = {
  name: 'all objects',
  id:"2e6ca1c3",      
  children: [
     {
         name: 'child',
         id: "6c03cfbe",
         children: [                 
             { name: 'grandchild1',
               id: "2790f59c"
             },
             { name: 'grandchild2',
               id: "52126f7d"
             },
             { name: 'grandchild3',
               id: "b402f14b" 
             },
             {
               name: 'grandchild4',
               id: "6c03cff0",
               children: [
                 { name: 'great-grandchild1',
                   id: "ce90ffa6"
                 },
                 { name: 'great-grandchild2',
                   id: "52f95f28" 
                 }
              ]
            }
         ]
    },        
     {
       name: 'child2',
       id: "7693b310",
       children: [
          { name: 'grandchild5',
            id: "def86ecc"  
          },
          { name: 'grandchild6',
            id: "6224a8f8"
          }
       ]
    }
 ]
}
到头来

var objects = {
  name: 'all objects',
  id:"2e6ca1c3",      
  children: [
     {
         name: 'child',
         id: "6c03cfbe",
         children: [                 
             { name: 'grandchild1',
               id: "2790f59c"
             },
             { name: 'grandchild2',
               id: "52126f7d",
               children = [
                 { name: 'great-granchild3',
                   id: '2a12a10h'
                 },
                 { name: 'great-granchild4',
                   id: 'bpme7qw0'
                 }
               ]
             },
             { name: 'grandchild3',
               id: "b402f14b" 
             },
             {
               name: 'grandchild4',
               id: "6c03cff0",
               children: [
                 { name: 'great-grandchild1',
                   id: "ce90ffa6"
                 },
                 { name: 'great-grandchild2',
                   id: "52f95f28" 
                 }
              ]
            }
         ]
    },        
     {
       name: 'child2',
       id: "7693b310",
       children: [
          { name: 'grandchild5',
            id: "def86ecc"  
          },
          { name: 'grandchild6',
            id: "6224a8f8"
          }
       ]
    }
 ]
}
那么:

objects.children[0].children[1].children = children;

首先找到合适的节点

function getNodeById(id, node){
    var reduce = [].reduce;
    function runner(result, node){
        if(result || !node) return result;
        return node.id === id && node || //is this the proper node?
            runner(null, node.children) || //process this nodes children
            reduce.call(Object(node), runner, result);  //maybe this is some ArrayLike Structure
    }
    return runner(null, node);
}

var target = getNodeById("52126f7d", objects);
target.children = children;

可能的双重目标是单独的变量。如何在现有的
对象中插入
目标
?您不必插入它,它是对具有该id的特定节点的引用。此节点已在对象中。我想获得包含
目标
节点的完整树。我编辑了这个问题,请再看一遍。你们有并没有尝试执行我的例子?在我看来,你似乎对变量、对象和引用等基本概念缺乏理解。请尝试执行我的示例,然后告诉我它不起作用,您得到了什么结果,以及您期望的结果。请注意,getNodeById()可能返回null,如果它找不到匹配的节点,那么您的函数将中断,因为您无法处理此情况。