3

I want to add the children array to node where id = 52126f7d (or another). How do I do it?

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"
          }
       ]
    }
 ]
}

to end up with

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"
          }
       ]
    }
 ]
}
yak_ilnur
  • 95
  • 1
  • 10
  • possible duplidate http://stackoverflow.com/questions/18884840/adding-a-new-array-element-to-a-json-object – lordkain Jan 20 '16 at 15:36

2 Answers2

8

by finding the proper node first.

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;
Thomas
  • 3,513
  • 1
  • 13
  • 10
  • target is separate variable. how to insert `target` in existing `objects`? – yak_ilnur Jan 20 '16 at 16:44
  • you don't have to insert it, it is a reference to the particular node with that id. This node already is in objects. – Thomas Jan 21 '16 at 02:28
  • i want to get a full tree with `target` nodes. I edited question, please look again. – yak_ilnur Jan 21 '16 at 12:50
  • did you even try to execute my example? It seems to me that you lack an understanding of such basic concepts like what are Variables, Objects and References. Pls try to execute my example and then tell me that it didn't work, what result you got, and what you expected. – Thomas Jan 21 '16 at 14:16
  • sorry, it was my mistake... in my function `addChildren(children, parent_id){ var target = this.getNodeById(parent_id, objectsList); target.children = children; return objectsList; }` I returned `target` :( – yak_ilnur Jan 21 '16 at 14:42
  • 1
    Beware, getNodeById() may return null, if it doesn't find a matching node, then your function will break, cause you don't handle this case. – Thomas Jan 21 '16 at 15:02
-6

How about:

objects.children[0].children[1].children = children;
DougieHauser
  • 460
  • 6
  • 14