【问题标题】:json getting children's children nodes hierarchyjson获取孩子的孩子节点层次结构
【发布时间】:2021-01-04 11:06:12
【问题描述】:

我正在努力让孩子的孩子使用这个 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();

enter image description here

【问题讨论】:

    标签: d3.js nodes parent hierarchy


    【解决方案1】:

    在当前状态下,您的深度为 2 层。因此缺少外环。

    children.push(node); 将 json 对象完整地添加到 children 数组中。

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

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

    缺少的代码,要在上一行之后添加:

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

    将第三层添加到您的数组中。

    【讨论】:

    • 谢谢,这很有意义!我试图合并代码但仍然没有得到外层: function getChildren(node){ var children = []; children.push(节点); //遍历子节点 node.children.forEach(function(child){ //如果目标 id 不同则添加到子列表 children.push(child); child.children.forEach(function(innerChild){ if ( innerChild.children) { children.push(innerChild.children); } }); });返回孩子 }
    • 外环的元素还没有加入数组吗?您能否将一个节点及其所有子节点添加到您的问题(相关 json 的一部分)?
    • 我刚刚用我现在拥有的内容更新了问题中的代码 - 也许我仍然缺少一些东西 - 突出显示了第一个节点,但没有突出显示第三个外层(参见问题所附的图片)感谢您的帮助!
    • 我认为这是在抱怨嵌套的 forEach 循环。我收到错误“javaScriptConsoleMessage: message=Uncaught TypeError: Cannot read property 'forEach' of undefined sourceId=localhost:8005/script.jslineNumber=271”
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-09-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-09-27
    • 2018-08-10
    相关资源
    最近更新 更多