D3.js json获得儿童';子节点层次结构

D3.js json获得儿童';子节点层次结构,d3.js,nodes,parent,hierarchy,D3.js,Nodes,Parent,Hierarchy,我正在努力让孩子们的孩子在这个json代码,任何帮助将不胜感激! 基本上,我在D3中有一个3层的太阳暴流图,我想突出显示当前悬停的节点,以及它的子节点和外环子节点。当前代码有效,但仅高亮显示最近的子项: function getChildren(node){ var children = []; children.push(node); //iterate through the children node.children.forEach(func

我正在努力让孩子们的孩子在这个json代码,任何帮助将不胜感激! 基本上,我在D3中有一个3层的太阳暴流图,我想突出显示当前悬停的节点,以及它的子节点和外环子节点。当前代码有效,但仅高亮显示最近的子项:

  function getChildren(node){
  var children = [];
   children.push(node);
        //iterate through the children
        node.children.forEach(function(child){
            //add to the child list if target id is not same
            children.push(child);
            
            children.push(child.children);

 ////Below is my attempt to add second layer of child elements but it isn't working as planned:

            child.children.forEach(function(innerChild){
            if (innerChild.children) {
                children.push(innerChild.children);
                                      }
                                                        });
                                             });
            return children

      }
层次结构如下:

  result = _(dataArr)
              .groupBy(x => x["Inner circle"])
              .map((value1, key) => ({
                name: key, count: sum(value1), children: _(value1)
                  .groupBy(x => x["Middle circle"])
                  .map((value2, key) => ({
                    name: key, count: sum(value2), children: _(value2)
                      .groupBy(x => x["Outer circle"])
                      .map((value3, key) => ({
                        name: key, count: sum(value3), children: _(value3)
                        .groupBy(x => x["Label"])
                        .map((value4, key) => ({ name: key, count: outersum(value4) , children: [] }))

                          .value()
                  }))
                  .value()
              }))
              .value()
            }))
            .value();

在当前状态下,您有两层。因此,丢失的外圈

children.push(节点)
将json对象全部添加到
子数组中

children.push(child)
将json对象的所有子对象添加到
子对象
数组中。这是第一层或内层

children.push(child.children)添加内圈的所有子项。这是第二层

缺少的代码,将添加在前一行之后:

child.children.forEach(function(innerChild){
  if (innerChild.children) {
    children.push(innerChild.children);
  }
});

将第三层添加到阵列中。

在当前状态下,您有两层。因此,丢失的外圈

children.push(节点)
将json对象全部添加到
子数组中

children.push(child)
将json对象的所有子对象添加到
子对象
数组中。这是第一层或内层

children.push(child.children)添加内圈的所有子项。这是第二层

缺少的代码,将添加在前一行之后:

child.children.forEach(function(innerChild){
  if (innerChild.children) {
    children.push(innerChild.children);
  }
});

将向阵列中添加第三层。

谢谢,这很有意义!我尝试合并代码,但是仍然没有得到外层:函数getChildren(node){var children=[];children.push(node);//迭代子节点.children.forEach(函数(child){//如果目标id不是相同的children.push(child),则添加到子列表中;child.children.forEach(function(innerChild){if(innerChild.children){children.push(innerChild.children);}}}});return children}外环的元素还没有添加到数组中吗?你能在你的问题(相关json的一部分)中添加一个节点及其所有子节点吗?我刚刚用我现在拥有的更新了问题中的代码-可能还有一些我仍然缺少的东西-前两个节点高亮显示,但没有第三个外层(见问题所附图片),谢谢你的帮助!我认为这是在抱怨嵌套的forEach循环。我收到错误“javaScriptConsoleMessage:message=UncaughtTypeError:无法读取未定义sourceId=lineNumber=271的属性'forEach'”谢谢,这很有意义!我尝试合并代码,但是仍然没有得到外层:函数getChildren(node){var children=[];children.push(node);//迭代子节点.children.forEach(函数(child){//如果目标id不是相同的children.push(child),则添加到子列表中;child.children.forEach(function(innerChild){if(innerChild.children){children.push(innerChild.children);}}}});return children}外环的元素还没有添加到数组中吗?你能在你的问题(相关json的一部分)中添加一个节点及其所有子节点吗?我刚刚用我现在拥有的更新了问题中的代码-可能还有一些我仍然缺少的东西-前两个节点高亮显示,但没有第三个外层(见问题所附图片),谢谢你的帮助!我认为这是在抱怨嵌套的forEach循环。我收到错误“javaScriptConsoleMessage:message=UncaughtTypeError:无法读取未定义sourceId=lineNumber=271的属性'forEach'”