var json = {
"text" : "表格列名",
"children" : [{
"text" : "序号",
"children" : [{
"text" : "序号一",
"children" : []
},{
"text" : "序号二",
"children" : []
}]
},{
"text" : "名称",
"children" : []
},{
"text" : "项目",
"children" : [{
"text" : "项目一",
"children" : [{
"text" : "项目二",
"children" : []
}]
}]
}]
};
//colspan记录节点的所有叶子节点个数
function getLeafCountForTree(json) {
if(json.children.length == 0){
json.colspan = 1;
return 1;
}else{
var leafCount = 0;
for(var i = 0 ; i < json.children.length ; i++){
leafCount = leafCount + getLeafCountTree(json.children[i]);
}
json.colspan = leafCount;
return leafCount;
}
}
getLeafCountForTree(json);
js递归树结构计算每个节点的叶子节点的数量并且输出
最新推荐文章于 2024-07-24 11:09:48 发布