所以我有一个像下面的树结构,我在叶节点中有一个count属性。我想总结一下计数,并将计数总和与其父项相对应。所以parent1和parent2有他们孩子的总和。而且从那里继续,所以grantparent具有parent1和parent2的总和。我也有遍历树的功能。但得到计数我无法得到。
有什么想法吗?
function transverse(element, result, isSegmentData) {
if (element instanceof Array){
element.forEach(function (item) {
{ transverse(item, result, isSegmentData); }
});
}
else if (element instanceof Object) {
if (element.hasOwnProperty("count")) {
// sum the count and provide to parent
}
if (element.hasOwnProperty("childNodes")) {
transverse(element.childNodes, result, isSegmentData);
}
}
}
[
{
"nodeId": 66318,
"nodeName": "grand parent",
"childNodes": [
{
"nodeId": 66323,
"nodeName": "parent1",
"childNodes": [
{
"nodeId": 66324,
"nodeName": "child1",
"childNodes": [],
"count": 25
},
{
"nodeId": 66334,
"nodeName": "child2",
"childNodes": [],
"count": 85
},
{
"nodeId": 66439,
"nodeName": "child3",
"childNodes": [],
"count": 65
},
{
"nodeId": 66462,
"nodeName": "child4",
"childNodes": [],
"count": 954
}
]
},
{
"nodeId": 66323,
"nodeName": "parent2",
"childNodes": [
{
"nodeId": 66324,
"nodeName": "child1",
"childNodes": [],
"count": 225
},
{
"nodeId": 66334,
"nodeName": "child2",
"childNodes": [],
"count": 815
}
]
}
]
}
]
答案 0 :(得分:2)
您可以使用命名函数并再次为数组调用它。然后分配计数,如果没有给出,从孩子那里得到计数。
var data = [{ "nodeId": 66318, "nodeName": "grand parent", "childNodes": [{ "nodeId": 66323, "nodeName": "parent1", "childNodes": [{ "nodeId": 66324, "nodeName": "child1", "childNodes": [], "count": 25 }, { "nodeId": 66334, "nodeName": "child2", "childNodes": [], "count": 85 }, { "nodeId": 66439, "nodeName": "child3", "childNodes": [], "count": 65 }, { "nodeId": 66462, "nodeName": "child4", "childNodes": [], "count": 954 }] }, { "nodeId": 66323, "nodeName": "parent2", "childNodes": [{ "nodeId": 66324, "nodeName": "child1", "childNodes": [], "count": 225 }, { "nodeId": 66334, "nodeName": "child2", "childNodes": [], "count": 815 }] }] }];
data.reduce(function x(r, a) {
a.count = a.count || Array.isArray(a.childNodes) && a.childNodes.reduce(x, 0) || 0;
return r + a.count;
}, 0);
document.write('<pre>' + JSON.stringify(data, 0, 4) + '</pre>');
console.log(data);
&#13;
.as-console-wrapper { max-height: 100% !important; top: 0; }
&#13;
答案 1 :(得分:1)
如果我正确地理解了你的问题,你想遍历树并协调所有节点的计数,这样每个节点的计数等于其所有后代的计数(如果有的话)。如果是这种情况,尝试这个功能(注意,我没有测试它,但它应该做你想要的):
function reconcileCount(node) {
var summedCountOfChildren = node.childNodes.reduce(function(_summedCount, childNode) {
var childCount = childNode.childNodes && childNode.childNodes.length > 0 ? reconcileCount(childNode) : childNode.count;
return _summedCount + (childCount || 0);
}, 0);
node.count = summedCountOfChildren;
return node.count;
}
基本上,它需要一个节点并且将协调传递的节点以及作为传递的节点的后代的所有节点(即,您可以传递您的树,假设树具有一个父节点,并且它将协调您的整个树)。
答案 2 :(得分:1)
你必须通过调用相同的函数对元素进行递归循环并求和子函数,这里是:
function sumNodes(nodeList) {
var sumChildren = function(node) {
var sum = 0;
for (var i = 0; i < node.childNodes.length && node.childNodes != null; i++) {
sum += sumChildren(node.childNodes[i]);
}
node.sum = sum;
return node.count == undefined ? sum : node.count;
}
for(var i=0; i<nodeList.length; i++){
sumChildren(nodeList[i]);
}
return nodeList;
}
以树为参数调用sumNodes
函数。
var treeWithSum = sumNodes(tree);
console.log(treeWithSum); // to see the results in the log
var data = [{
"nodeId": 66318,
"nodeName": "grand parent",
"childNodes": [{
"nodeId": 66323,
"nodeName": "parent1",
"childNodes": [{
"nodeId": 66324,
"nodeName": "child1",
"childNodes": [{
"nodeId": 66324,
"nodeName": "child1",
"childNodes": [],
"count": 25
}, {
"nodeId": 66324,
"nodeName": "child1",
"childNodes": [{
"nodeId": 66324,
"nodeName": "child1",
"childNodes": [],
"count": 25
}, {
"nodeId": 66324,
"nodeName": "child1",
"childNodes": [{
"nodeId": 66324,
"nodeName": "child1",
"childNodes": [],
"count": 25
}, {
"nodeId": 66324,
"nodeName": "child1",
"childNodes": [],
"count": 25
}]
}]
}]
}, {
"nodeId": 66334,
"nodeName": "child2",
"childNodes": [],
"count": 85
}, {
"nodeId": 66439,
"nodeName": "child3",
"childNodes": [],
"count": 65
}, {
"nodeId": 66462,
"nodeName": "child4",
"childNodes": [],
"count": 954
}]
}, {
"nodeId": 66323,
"nodeName": "parent2",
"childNodes": [{
"nodeId": 66324,
"nodeName": "child1",
"childNodes": [],
"count": 225
}, {
"nodeId": 66334,
"nodeName": "child2",
"childNodes": [],
"count": 815
}]
}]
}]
function sumNodes(nodeList) {
var sumChildren = function(node) {
var sum = 0;
for (var i = 0; i < node.childNodes.length && node.childNodes != null; i++) {
sum += sumChildren(node.childNodes[i]);
}
node.sum = sum;
return node.count == undefined ? sum : node.count;
}
for(var i=0; i<nodeList.length; i++){
sumChildren(nodeList[i]);
}
return nodeList;
}
$("#results").html(JSON.stringify(sumNodes(data), null, 4));
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<pre id="results"></pre>