<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>json-query</title>
<script type="text/javascript">
const arr = [
{
"name": "aaa",
"Children": [
{
"name": "aaa",
"Children": [
{
"name": "ccc",
'type': 'none',
"Children": [
{
"name": "ccc",
"Children": []
}
]
},
{
"name": "eee",
"Children": []
}
]
}
]
}]
</script>
<script type="text/javascript">
function getLeafCountTree(data) {
if (data.Children.length == 0) {
data.colspan = 1;
return 1;
} else {
var leafCount = 0;
for (var i = 0; i < data.Children.length; i++) {
leafCount = leafCount + getLeafCountTree(data.Children[i]);
}
data.colspan = leafCount;
if (data.type === "none") {//特殊节点不计算在内
return leafCount;
} else {
return leafCount + 1;
}
}
}
console.log(getLeafCountTree(arr[0]))
</script>
</head>
<body>
</body>
</html>
效果图: