Reputation: 989
I'm working with a nested JSON feed with D3.js
My code works fine when the child object is named children
, but I want to be able display nodes for a few of other objects too, not just ones named children
.
For example, if within the children
object, I have another object named options
. I want to display nodes for that object as well.
{
"item": [{
"children": [{
"name": "banana",
"definition": "this is a fruit",
"group": "n",
"options": [
{
"color": "red",
"shape": "square"
}
],
"countries": [
{
"color": "america",
"shape": "africa"
}
]
},
{
"name": "apple",
"definition": "this is a fruit",
"group": "n",
"options": [
{
"color": "red",
"shape": "square"
}
]
}]
}]
}
Here is a recursive function I have within my flatten function:
// Returns a list of all nodes under the root.
function flatten(root) {
var nodes = [], i = 0;
function recurse(node) {
if (node.children) {
node.size = node.children.reduce(function(p, v) {
return p + recurse(v);
}, 0);
}
if (!node.id) node.id = ++i;
nodes.push(node);
return node.size;
}
root.size = recurse(root);
return nodes;
}
Does anyone have any idea how one would go about this please?
Upvotes: 3
Views: 588
Reputation: 4639
This question really doesn't have anything to do with jQuery or D3; it's just plain JavaScript and JSON.
If you just want your code to work with any other array in your JSON object, then it's simply a matter of replacing your if statement where you check for d["children"]
to go through all the attributes of the JSON object and recurse on anything that is an array instead. Something like this:
function flatten(root) {
var nodes = [], i = 0;
function recurse(node) {
for (var x in node) {
if (Array.isArray(node[x])) {
node.size = node[x].reduce(function(p, v) {
return p + recurse(v);
}, 0);
}
}
if (!node.id) node.id = ++i;
nodes.push(node);
return node.size;
}
root.size = recurse(root);
return nodes;
}
Upvotes: 3