javascript - 在 json 树中通过 id 查找节点

标签 javascript json javascript-objects

我想将 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"
          }
       ]
    }
 ]
}

最佳答案

首先找到合适的节点。

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;

关于javascript - 在 json 树中通过 id 查找节点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34903361/

相关文章:

javascript - 理解 JavaScript 中的 new Object()

javascript - Lodash/javascript : Compare two collections and return the differences

javascript - 为什么我的 JSON 不起作用

javascript - 如何在没有重定向的情况下使用空的 href 属性?

javascript - 将 json 数据从两个对象编辑为一个对象

java - 将javax.ws.rs实体序列化为json

json - 在 ARM 模板中有条件添加应用程序设置

javascript - 如何将 Javascript 中的对象数组解构为 ES6 中的两个预定义变量?

javascript - 跨越多个非文本节点时突出显示页面上的选定文本

javascript - 这里的pos+=1有什么用?