我们有一个JSON树结构,用于维护特定节点的子节点。这个children参数是Undefined(无子项)或数组。
我想计算节点的分数,这是子节点分数的总和。没有孩子的节点得分为1。
这对我来说似乎很简单,但只有叶子节点中的最后一个节点才能正确更新,其余节点显示为NaN。我不确定Undefined / NaN在哪里被引入,我怀疑它可能是由于JS如何在递归中处理引用?
function updateScore(n) {
if (n.children == undefined) return 1;
n.children.forEach(function(c){
var r = updateScore(c);
//if (!r) console.log(n);
n.score += r;
});
}
updateScore(sparent);
数据如下:
var sparent = {"id":"src","score":0,"children":[{"id":0,"score":0,"children":[{"id":14,"score":0}]},{"id":2,"score":0},{"id":4,"score":0},{"id":11,"score":0},{"id":17,"score":0,"children":[{"id":31,"score":0}]},{"id":18,"score":0},{"id":23,"score":0,"children":[{"id":13,"score":0,"children":[{"id":10,"score":0},{"id":21,"score":0,"children":[{"id":16,"score":0}]},{"id":82,"score":0,"children":[{"id":22,"score":0},{"id":75,"score":0}]},{"id":91,"score":0}]},{"id":48,"score":0,"children":[{"id":70,"score":0,"children":[{"id":46,"score":0},{"id":74,"score":0}]},{"id":97,"score":0,"children":[{"id":25,"score":0}]}]}]},{"id":28,"score":0,"children":[{"id":85,"score":0}]},{"id":30,"score":0,"children":[{"id":24,"score":0,"children":[{"id":26,"score":0},{"id":53,"score":0,"children":[{"id":45,"score":0}]},{"id":76,"score":0,"children":[{"id":38,"score":0},{"id":62,"score":0},{"id":66,"score":0}]},{"id":93,"score":0,"children":[{"id":1,"score":0},{"id":56,"score":0}]}]},{"id":78,"score":0,"children":[{"id":50,"score":0,"children":[{"id":15,"score":0},{"id":67,"score":0}]}]},{"id":94,"score":0}]},{"id":36,"score":0},{"id":39,"score":0,"children":[{"id":35,"score":0,"children":[{"id":73,"score":0},{"id":83,"score":0,"children":[{"id":3,"score":0}]}]}]},{"id":41,"score":0,"children":[{"id":5,"score":0},{"id":32,"score":0}]},{"id":43,"score":0,"children":[{"id":20,"score":0,"children":[{"id":89,"score":0,"children":[{"id":52,"score":0}]}]},{"id":27,"score":0},{"id":86,"score":0,"children":[{"id":6,"score":0}]},{"id":98,"score":0,"children":[{"id":54,"score":0},{"id":59,"score":0},{"id":65,"score":0,"children":[{"id":8,"score":0},{"id":47,"score":0}]},{"id":79,"score":0}]}]},{"id":64,"score":0},{"id":71,"score":0,"children":[{"id":19,"score":0},{"id":51,"score":0,"children":[{"id":57,"score":0,"children":[{"id":29,"score":0,"children":[{"id":49,"score":0}]},{"id":60,"score":0,"children":[{"id":34,"score":0}]}]},{"id":61,"score":0,"children":[{"id":63,"score":0},{"id":72,"score":0,"children":[{"id":9,"score":0},{"id":37,"score":0}]}]},{"id":90,"score":0}]},{"id":92,"score":0,"children":[{"id":96,"score":0,"children":[{"id":55,"score":0,"children":[{"id":12,"score":0},{"id":69,"score":0}]},{"id":80,"score":0}]}]}]},{"id":77,"score":0,"children":[{"id":81,"score":0,"children":[{"id":40,"score":0}]}]},{"id":84,"score":0,"children":[{"id":7,"score":0,"children":[{"id":33,"score":0,"children":[{"id":42,"score":0},{"id":95,"score":0}]}]},{"id":44,"score":0,"children":[{"id":58,"score":0}]}]},{"id":87,"score":0,"children":[{"id":68,"score":0}]},{"id":88,"score":0},{"id":99,"score":0}]};
答案 0 :(得分:3)
如果元素有子元素,则忘记返回score
。试试这个:
function updateScore(n) {
if (n.children == undefined) return 1;
n.children.forEach(function(c){
var r = updateScore(c);
//if (!r) console.log(n);
n.score += r;
});
return n.score;
}