将嵌套对象映射为包含D3树形图图形的键“名称”和“子级”

时间:2019-01-31 04:49:59

标签: javascript d3.js treemap

此问题与以下内容相似:d3.nest() key and values conversion to name and children

但是,我的数据(来自JSON文件)具有以下格式:

{
  "Key1": [
    "child1",
    "child2",
    {
      "key1Sub1": [
        "child1",
        "child2",
        {
          "key1sub1sub": [
            "child1",
            "child2"
          ]
        },
        {
          "key1sub2": [
            "child1",
            "child2"
          ]
        },
        {
          "key1sub3": [
            "child1",
            "child2"
          ]
        }
      ]
    }
  ]
}

我将如何使用地图功能来格式化此类对象,或者如何编辑树图源以获取如下格式:

{
  "name": "Top Level",
  "children": [
    { 
      "name": "Level 2: A",
      "children": [
        { "name": "Son of A" },
        { "name": "Daughter of A" }
      ]
    },
    { "name": "Level 2: B" }
  ]
}

0 个答案:

没有答案