Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/428.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript D3.js树布局-要获取子体的计数吗_Javascript_D3.js - Fatal编程技术网

Javascript D3.js树布局-要获取子体的计数吗

Javascript D3.js树布局-要获取子体的计数吗,javascript,d3.js,Javascript,D3.js,我想知道如何获得节点的后代计数 我可以用这个代码得到孩子的数量 console.log(d.children.length); 但我怎样才能得到那个节点的后代数呢 我需要使用递归吗 任何帮助都将不胜感激。这是递归 函数getCountparent{ var计数=0; 如果Array.isArrayparent.children{ 计数+=parent.children.length; parent.children.forEachfunctionchild{ 如果Array.isArrayc

我想知道如何获得节点的后代计数

我可以用这个代码得到孩子的数量

console.log(d.children.length);
但我怎样才能得到那个节点的后代数呢

我需要使用递归吗


任何帮助都将不胜感激。

这是递归

函数getCountparent{ var计数=0; 如果Array.isArrayparent.children{ 计数+=parent.children.length; parent.children.forEachfunctionchild{ 如果Array.isArraychild.children{ count+=getCountchild; } }; } 返回计数; } 变量d={ 儿童:[ 1. { 儿童:[ 1. 2. ] }, 2. 3. ] };
console.loggetCountd;如果你有孩子的话,你的父母是不会计算的。如果A有子B,C和B有D,E,那么A的后代计数应该是4。但是你的输出应该是3,C,D,e,你需要这样使用它:getCountparent不像getCountparent.children。根据您的示例,您应该调用getCountd。