Reputation: 291
Suppose I have the following JSON:
{
"id": "foo",
"list": [
{
"id": "A",
"list": [
{
"id": "B",
"list": [
{
"id": "C",
"list": [
{
"id": "D",
"list": []
},
{
"id": "E",
"list": []
}
]
},
{
"id": "F",
"list": []
},
{
"id": "G",
"list": [
{
"id": "H",
"list": []
},
{
"id": "I",
"list": []
},
{
"id": "J",
"list": []
}
]
}
]
},
{
"id": "K",
"list": []
}
]
},
{
"id": "L",
"list": [
{
"id": "M",
"list": []
}
]
},
{
"id": "N",
"list": []
},
{
"id": "O",
"list": [
{
"id": "P",
"list": [
{
"id": "Q",
"list": []
},
{
"id": "R",
"list": []
},
{
"id": "S",
"list": []
},
{
"id": "T",
"list": [
{
"id": "U",
"list": []
}
]
},
{
"id": "V",
"list": [
{
"id": "W",
"list": [
{
"id": "X",
"list": []
},
{
"id": "Y",
"list": []
},
{
"id": "Z",
"list": []
}
]
}
]
}
]
}
]
}
]
}
My question is: How could I count each of the childs and append this number into a property of each object?
Example:
Regarding this, the "C" object should have a property, let's name it "allBelow", containing the number 2. The "W" object containing 3, and the "V" object containing 4. And so on, for each object.
I wonder that some recursive function could do this job but I'm not achieving it.
Could you please help me?
Bests,
Upvotes: 1
Views: 759
Reputation: 31692
var myObj = {"id":"foo","list":[{"id":"A","list":[{"id":"B","list":[{"id":"C","list":[{"id":"D","list":[]},{"id":"E","list":[]}]},{"id":"F","list":[]},{"id":"G","list":[{"id":"H","list":[]},{"id":"I","list":[]},{"id":"J","list":[]}]}]},{"id":"K","list":[]}]},{"id":"L","list":[{"id":"M","list":[]}]},{"id":"N","list":[]},{"id":"O","list":[{"id":"P","list":[{"id":"Q","list":[]},{"id":"R","list":[]},{"id":"S","list":[]},{"id":"T","list":[{"id":"U","list":[]}]},{"id":"V","list":[{"id":"W","list":[{"id":"X","list":[]},{"id":"Y","list":[]},{"id":"Z","list":[]}]}]}]}]}]};
function count(obj) {
var c = obj.list.length;
c += obj.list.reduce((a, e) => a + count(e), 0);
obj.count = c; // assign the count after counting the subobjects.
return c; // return the count to be used by parent objects
}
count(myObj);
console.log(myObj);
Upvotes: 2
Reputation: 7385
A recursive function is a good idea. Try this:
var data = {"id":"foo","list":[{"id":"A","list":[{"id":"B","list":[{"id":"C","list":[{"id":"D","list":[]},{"id":"E","list":[]}]},{"id":"F","list":[]},{"id":"G","list":[{"id":"H","list":[]},{"id":"I","list":[]},{"id":"J","list":[]}]}]},{"id":"K","list":[]}]},{"id":"L","list":[{"id":"M","list":[]}]},{"id":"N","list":[]},{"id":"O","list":[{"id":"P","list":[{"id":"Q","list":[]},{"id":"R","list":[]},{"id":"S","list":[]},{"id":"T","list":[{"id":"U","list":[]}]},{"id":"V","list":[{"id":"W","list":[{"id":"X","list":[]},{"id":"Y","list":[]},{"id":"Z","list":[]}]}]}]}]}]};
function addCount(node) {
node.count = 0;
for (var i = 0; i < node.list.length; i++) {
var child = node.list[i];
addCount(child);
node.count += child.count + 1;
}
}
addCount(data);
console.log(data)
It first calls itself on each child node. Then it adds each child to the count as 1 + the number of grandchildren (or grand-grand- or more).
Upvotes: 0
Reputation: 3436
A depth first search should work, I'm thinking something like this (UNTESTED CODE):
function DFS(tree){
var currentCount = tree.list.length;
for(var i=0;i<count;i++){
currentCount += DFS(tree.list[i]);
}
tree["count"] = currentCount;
return currentCount;
}
Upvotes: 0
Reputation: 8981
You can do a simple DFS:
function appendNumChildren(currentNode) {
const totalChildren = currentNode.list.reduce((acc, node) => {
return acc + appendNumChildren(node);
}, 0)
currentNode.allBelow = totalChildren;
return totalChildren + 1;
}
appendNumChildren(json);
https://jsbin.com/qekabatuwi/edit?js,console
Upvotes: 2