javascript - 获取树的每个节点下的子节点数
问题描述
我正在从 json 数据制作一棵树:
{
"text":{
"name":"test41 (3256)",
"balance":"Total Balance: 0",
"profit":"Total Profit: 0"
},
"image":"treant-js-master/images/1621593159.jpg",
"id":"3256",
"parentId":"3243",
"children":[
{
"text":{
"name":"test45 (3261)",
"balance":"Total Balance: 0",
"profit":"Total Profit: 0"
},
"image":"treant-js-master/images/1621593159.jpg",
"id":"3261",
"parentId":"3256"
},
{
"text":{
"name":"test46 (3262)",
"balance":"Total Balance: 0",
"profit":"Total Profit: 0"
},
"image":"treant-js-master/images/1621593159.jpg",
"id":"3262",
"parentId":"3256",
"children":[
{
"text":{
"name":"test51 (3267)",
"balance":"Total Balance: 0",
"profit":"Total Profit: 0"
},
"image":"treant-js-master/images/1621593159.jpg",
"id":"3267",
"parentId":"3262"
}
]
},
{
"text":{
"name":"test47 (3263)",
"balance":"Total Balance: 0",
"profit":"Total Profit: 0"
},
"image":"treant-js-master/images/1621593159.jpg",
"id":"3263",
"parentId":"3256"
}
]
}
由这些数据生成的树如下所示(我正在使用 treant.js 库来生成树);
现在,我想在每个节点下方显示子节点的数量。所以,我想要一个可以计算每个节点的子节点数量并在数组数据中的每个节点“文本”参数中修改其值的逻辑。上述树的数组如下所示:
Array ( [0] => Array ( [text] => Array ( [name] => test41 (3256) [balance] => Total Balance: 0 [profit] => Total Profit: 0 ) [image] => treant-js-master/images/1621593159.jpg [id] => 3256 [parentId] => 3243 [children] => Array ( [0] => Array ( [text] => Array ( [name] => test45 (3261) [balance] => Total Balance: 0 [profit] => Total Profit: 0 ) [image] => treant-js-master/images/1621593159.jpg [id] => 3261 [parentId] => 3256 ) [1] => Array ( [text] => Array ( [name] => test46 (3262) [balance] => Total Balance: 0 [profit] => Total Profit: 0 ) [image] => treant-js-master/images/1621593159.jpg [id] => 3262 [parentId] => 3256 [children] => Array ( [0] => Array ( [text] => Array ( [name] => test51 (3267) [balance] => Total Balance: 0 [profit] => Total Profit: 0 ) [image] => treant-js-master/images/1621593159.jpg [id] => 3267 [parentId] => 3262 ) ) ) [2] => Array ( [text] => Array ( [name] => test47 (3263) [balance] => Total Balance: 0 [profit] => Total Profit: 0 ) [image] => treant-js-master/images/1621593159.jpg [id] => 3263 [parentId] => 3256 ) ) ) )
帮我建立一个可以解析这个数组的逻辑,计算每个节点的子节点,并用 [count] 个子节点修改“text”参数。
解决方案
推荐阅读
- c# - 使用卡付款时出现 PayPal 错误 10752 错误消息
- sql-server - 我们应该使用 bigint 作为人工密钥吗?
- javascript - 所有圆圈计数器显示相同的数据(如何让每个圆圈显示它自己的计数器数据值)
- c# - 如何在 HTML 的 HiddenField 值中添加新行?
- sql-server - 如何在excel工作簿中重置现有的sql server连接
- java - 在 SpringBoot 中使用嵌入式存根/虚拟应用程序进行测试
- amazon-web-services - 有没有办法将 CloudWatch 指标流式传输到 Lambda?
- javascript - 如何对集合中的多个值进行比较?
- azure - 如何从 Azure 区块链即服务中提取仲裁日志文件
- android - 如何提高将重图像上传到服务器的性能