0

我如何循环遍历可能看起来像这样的树形图 json 对象:并且只返回部分节点作为与每个部分节点下的节点相对应的节点?所以,一个人可能有一个看起来像这样的结构:

{
  name: "products"
  children:[
    {
      name: "Wood", 
      children: [
        { name: "rose wood" },
        { name: "maple" }
      ]
    },
    {
      name: "Metals", 
      children: [
        { name: "Iron" },
        { name: "copper" }
      ]
    }
  ]
}
//end

想要的是只返回“木材”、“金属”等。由于数据的冗余性,直接循环不会削减它。有任何想法吗?谢谢。

4

1 回答 1

1

这是我使用解决我的 JSON 解析问题的方法的代码。

.on("mouseover",function (d) {
     $("#treemapPopup").show().html(
              "<h4>"+d.parent.name +"</h4>"+
                  "<p><strong>Good:</strong> "+d.name+"<br />"+
                  //Math.round(percent* 10)/10+"% <br />"+
                  "<strong>Countries:</strong></p>"+
                    courtryList
          );
}) ...etc

当我遍历子对象数组时,d.parent.name 最终成为 Wood、Metals 等。当然,d.name 是 Wood 或 Metal 下的产品。希望能帮助其他可能一直坚持这一点的人。

于 2014-01-14T20:05:24.833 回答