I have some JavaScript code I have inherited. It draws a hierarchical tree based on json data (using D3 version 3 library). The code works. I need to make a minor modification, but HOW to make the modification is the problem. I have an object called treeData. treeData is a hierarchical listing of nodes in the tree. Each node has properties, such as name, id, depth, x, y, and children. Children is an array, which points to child nodes of the current node. So children can have children, can have children, etc. If I look at the treeData object, I see the 'root' node, and I see that it has 10 children.
The children array is structurally the same as the parent... This shows expanding the first child, and I see that it has 3 children. My data is typically 3-4 levels deep.
My challenge is that I need to run a function against all nodes in the tree. I am sure that there is an elegant way of doing this, but I don't know what it would be... I need something like..
var myNode;
for (var zz = 0; zz <= treeData.children.length; zz++) {
myNode = treeData.children[zz];
collapseNode(myNode);
}
The problem is that this just looks at one level, not traversing the hierarchy... How do I rewrite this to visit each node in the hierarchy?