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

2 Answers 2

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;
Sign up to request clarification or add additional context in comments.

6 Comments

target is separate variable. how to insert target in existing objects?
you don't have to insert it, it is a reference to the particular node with that id. This node already is in objects.
i want to get a full tree with target nodes. I edited question, please look again.
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.
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.
|
-6

How about:

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.