假设我有以下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": []
}
]
}
]
}
]
}
]
}
]
}
我的问题是:我如何计算每个孩子并将这个数字附加到每个物体的属性中?
示例:
对此,“C”对象应该有一个属性,让它命名为“allBelow”,包含数字2.“W”对象包含3,“V”对象包含4.依此类推,每个对象。
我想知道一些递归函数可以完成这项工作,但我没有实现它。
你能帮我吗?
贝斯茨,
答案 0 :(得分:2)
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);

答案 1 :(得分:1)
你可以做一个简单的DFS:
function appendNumChildren(currentNode) {
const totalChildren = currentNode.list.reduce((acc, node) => {
return acc + appendNumChildren(node);
}, 0)
currentNode.allBelow = totalChildren;
return totalChildren + 1;
}
appendNumChildren(json);
答案 2 :(得分:0)
深度优先搜索应该有效,我在想这样的事情( 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;
}
答案 3 :(得分:0)
递归函数是个好主意。试试这个:
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)
&#13;
它首先在每个子节点上调用自己。然后它将每个孩子的数量增加为1 +孙子的数量(或者是孙子或更多)。