获取树的每个节点下的子节点数

时间:2021-06-14 07:29:30

标签: javascript php arrays tree

我正在用 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 库来制作树);

enter image description here

现在,我想在每个节点下方显示子节点的数量。所以,我想要一个可以计算每个节点的子节点数量并在数组数据中的每个节点“文本”参数中修改其值的逻辑。上面树的数组看起来像这样:

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”参数。

0 个答案:

没有答案
相关问题