I would like to use the hierarchical structure of JSON, in a d3 tree layout such as in:

http://bl.ocks.org/1249394

In my work, I am using a variable in javascript:

var treeData = {
  "name" : "A1", "children" : [
    {"name" : "A3", "children": [
      {"name" : "A31", "children" :[
        {"name" : "A311" },
        {"name" : "A312" }
      ]}
    ]}
  ]};

How do I use this hierarchy as a dynamic javascript array, so that values can be pulled and new layers/branches in the hierarchy can be added or removed dynamically? Does this require a loop or empty arrays? I want to be able to create this hierarchy. My main problem is that I don't know how to keep the hierarchical structure in the javascript array code.

share|improve this question

What is the question exactly? – dbaseman Sep 20 at 1:25
The code you've shown is a JS object with nested arrays of objects, it is not JSON (which is a string representation, i.e., a serialisation of objects/arrays). If you're asking how to add and remove properties from objects you could probably get some tips from the MDN article Working With Objects. – nnnnnn Sep 20 at 1:28
smh. I am sorry for that, yes it is a JS object. I made that mistake as I took the hierarchy out of a JSON file. Basically, I would like to create a nested set of arrays, to reflect this hierarchy structure as shown by the object above. But, I would like to do so dynamically. Any ideas? – user1684586 Sep 20 at 1:47
feedback

1 Answer

up vote 0 down vote accepted

If I understand your question correctly, after you assign value to treeData as you did above, it's already the hierachy you want. To manipulate them easier, you can do something like

tree.nodes(treeData);

to get an array of objects

share|improve this answer
feedback

Your Answer

 
or
required, but never shown
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.