一尘不染

将平面JSON文件转换为分层的json数据,例如flare.json [d3示例文件]

json

经过一番麻烦的战斗之后,我几乎想出了如何将平面json文件转换为Hierarchical文件。我不是自己写函数的。

但是现在的问题是,在帖子中编写的函数只有两个层次结构。但是我正在寻找4个层次的层次结构。我尝试覆盖失败的功能,但是。

用我正在尝试的代码。


         var data = [
        { "dep": "First Top", "name": "First child", "model": "value1", "size": "320" },
        { "dep": "First Top", "name": "First child", "model": "value2", "size": "320" },
        { "dep": "First Top", "name": "First child", "model": "value3", "size": "320" },
        { "dep": "First Top", "name": "First child", "model": "value4", "size": "320" },
        { "dep": "First Top", "name": "SECOND CHILD", "model": "value1", "size": "320" },
        { "dep": "First Top", "name": "SECOND CHILD", "model": "value2", "size": "320" },
        { "dep": "First Top", "name": "SECOND CHILD", "model": "value3", "size": "320" },
        { "dep": "First Top", "name": "SECOND CHILD", "model": "value4", "size": "320" },
        { "dep": "Second Top", "name": "First Child", "model": "value1", "size": "320" },
        { "dep": "Second Top", "name": "First Child", "model": "value2", "size": "320" },
        { "dep": "Second Top", "name": "First Child", "model": "value3", "size": "320" },
        { "dep": "Second Top", "name": "First Child", "model": "value4", "size": "320" },
        { "dep": "Second Top", "name": "SECOND CHILD", "model": "value1", "size": "320" },
        { "dep": "Second Top", "name": "SECOND CHILD", "model": "value2", "size": "320" },
        { "dep": "Second Top", "name": "SECOND CHILD", "model": "value3", "size": "320" },
        { "dep": "Second Top", "name": "SECOND CHILD", "model": "value4", "size": "320" },
        { "dep": "Third Top", "name": "First Child", "model": "value2", "size": "320" },
        { "dep": "Third Top", "name": "First Child", "model": "value3", "size": "320" },
        { "dep": "Third Top", "name": "First Child", "model": "value4", "size": "320" },
        { "dep": "Third Top", "name": "First Child", "model": "value5", "size": "320" },
        { "dep": "Third Top", "name": "Second Child", "model": "value1", "size": "320" },
        { "dep": "Third Top", "name": "Second Child", "model": "value2", "size": "320" },
        { "dep": "Third Top", "name": "Second Child", "model": "value3", "size": "320" },
        { "dep": "Third Top", "name": "Second Child", "model": "value4", "size": "320" }
      ]

    var newData = {"name":"root", "children":{}}

    data.forEach(function(d){
        if(typeof newData.children[d.dep] !== 'undefined')  {
            newData.children[d.dep].children.push(d)
        } else {
            newData.children[d.dep] = {"name": d.dep, "children": [{"name": d.name, "children": [{"name": d.model, "size": d.size}]}]}
        }
    })





    newData.children = Object.keys(newData.children).map(function (key) { return newData.children[key]; });

              // show what we've got
              d3.select('body').append('pre')
                  .text(JSON.stringify(newData, null, '  '));


当前代码的输出

```

{
  "name": "root",
  "children": [
    {
      "name": "First Top",
      "children": [
        {
          "name": "First child",
          "children": [
            {
              "name": "value1",
              "size": "320"
            }
          ]
        },
        {
          "dep": "First Top",
          "name": "First child",
          "model": "value2",
          "size": "320"
        },
        {
          "dep": "First Top",
          "name": "First child",
          "model": "value3",
          "size": "320"
        },
        {
          "dep": "First Top",
          "name": "First child",
          "model": "value4",
          "size": "320"
        },
        {
          "dep": "First Top",
          "name": "SECOND CHILD",
          "model": "value1",
          "size": "320"
        },
        {
          "dep": "First Top",
          "name": "SECOND CHILD",
          "model": "value2",
          "size": "320"
        },
        {
          "dep": "First Top",
          "name": "SECOND CHILD",
          "model": "value3",
          "size": "320"
        },
        {
          "dep": "First Top",
          "name": "SECOND CHILD",
          "model": "value4",
          "size": "320"
        }
      ]
    },
    {
      "name": "Second Top",
      "children": [
        {
          "name": "First Child",
          "children": [
            {
              "name": "value1",
              "size": "320"
            }
          ]
        },
        {
          "dep": "Second Top",
          "name": "First Child",
          "model": "value2",
          "size": "320"
        },
        {
          "dep": "Second Top",
          "name": "First Child",
          "model": "value3",
          "size": "320"
        },
        {
          "dep": "Second Top",
          "name": "First Child",
          "model": "value4",
          "size": "320"
        },
        {
          "dep": "Second Top",
          "name": "SECOND CHILD",
          "model": "value1",
          "size": "320"
        },
        {
          "dep": "Second Top",
          "name": "SECOND CHILD",
          "model": "value2",
          "size": "320"
        },
        {
          "dep": "Second Top",
          "name": "SECOND CHILD",
          "model": "value3",
          "size": "320"
        },
        {
          "dep": "Second Top",
          "name": "SECOND CHILD",
          "model": "value4",
          "size": "320"
        }
      ]
    },
    {
      "name": "Third Top",
      "children": [
        {
          "name": "First Child",
          "children": [
            {
              "name": "value2",
              "size": "320"
            }
          ]
        },
        {
          "dep": "Third Top",
          "name": "First Child",
          "model": "value3",
          "size": "320"
        },
        {
          "dep": "Third Top",
          "name": "First Child",
          "model": "value4",
          "size": "320"
        },
        {
          "dep": "Third Top",
          "name": "First Child",
          "model": "value5",
          "size": "320"
        },
        {
          "dep": "Third Top",
          "name": "Second Child",
          "model": "value1",
          "size": "320"
        },
        {
          "dep": "Third Top",
          "name": "Second Child",
          "model": "value2",
          "size": "320"
        },
        {
          "dep": "Third Top",
          "name": "Second Child",
          "model": "value3",
          "size": "320"
        },
        {
          "dep": "Third Top",
          "name": "Second Child",
          "model": "value4",
          "size": "320"
        }
      ]
    }
  ]
}

```

所需的输出格式:

```

{
  "name": "root",
  "children": [
    {
      "name": "First Top",
      "children": [
        {
          "name": "First child",
          "children": [
            {
              "name": "value1",
              "size": "320"
            },
            {
              "name": "value2",
              "size": "320"
            },
            {
              "name": "value3",
              "size": "320"
            },
            {
              "name": "value4",
              "size": "320"
            }
          ]
        },
        {
          "name": "Second child",
          "children": [
            {
              "name": "value1",
              "size": "320"
            },
            {
              "name": "value2",
              "size": "320"
            },
            {
              "name": "value3",
              "size": "320"
            },
            {
              "name": "value4",
              "size": "320"
            }
          ]
        },

      ]
    },
    {
      "name": "Second Top",
      "children": [
        {
          "name": "First child",
          "children": [
            {
              "name": "value1",
              "size": "320"
            },
            {
              "name": "value2",
              "size": "320"
            },
            {
              "name": "value3",
              "size": "320"
            },
            {
              "name": "value4",
              "size": "320"
            }
          ]
        },
        {
          "name": "Second child",
          "children": [
            {
              "name": "value1",
              "size": "320"
            },
            {
              "name": "value2",
              "size": "320"
            },
            {
              "name": "value3",
              "size": "320"
            },
            {
              "name": "value4",
              "size": "320"
            }
          ]
        },
      ]
    },
    {
      "name": "Third Top",
      "children": [
        {
          "name": "First child",
          "children": [
            {
              "name": "value1",
              "size": "320"
            },
            {
              "name": "value2",
              "size": "320"
            },
            {
              "name": "value3",
              "size": "320"
            },
            {
              "name": "value4",
              "size": "320"
            }
          ]
        },
        {
          "name": "Second child",
          "children": [
            {
              "name": "value1",
              "size": "320"
            },
            {
              "name": "value2",
              "size": "320"
            },
            {
              "name": "value3",
              "size": "320"
            },
            {
              "name": "value4",
              "size": "320"
            }
          ]
        },
      ]
    }
  ]
}

```

我一个星期都在crack头,但我一个人也搞不清。有人请修改此功能,以便在更新后以分层格式获取数据。

提前致谢!!


阅读 321

收藏
2020-07-27

共1个答案

一尘不染

更新为使用递归方法

这应该适用于n级别,而不只是2或3。您只需要指定哪些属性定义哪些级别即可。

var data = [
    { "dep": "First Top", "name": "First child", "model": "value1", "size": "320" },
    { "dep": "First Top", "name": "First child", "model": "value2", "size": "320" },
    { "dep": "First Top", "name": "SECOND CHILD", "model": "value1", "size": "320" },
    { "dep": "Second Top", "name": "First Child", "model": "value1", "size": "320" }
];

var newData = { name :"root", children : [] },
    levels = ["dep","name"];

// For each data row, loop through the expected levels traversing the output tree
data.forEach(function(d){
    // Keep this as a reference to the current level
    var depthCursor = newData.children;
    // Go down one level at a time
    levels.forEach(function( property, depth ){

        // Look to see if a branch has already been created
        var index;
        depthCursor.forEach(function(child,i){
            if ( d[property] == child.name ) index = i;
        });
        // Add a branch if it isn't there
        if ( isNaN(index) ) {
            depthCursor.push({ name : d[property], children : []});
            index = depthCursor.length - 1;
        }
        // Now reference the new child array as we go deeper into the tree
        depthCursor = depthCursor[index].children;
        // This is a leaf, so add the last element to the specified branch
        if ( depth === levels.length - 1 ) depthCursor.push({ name : d.model, size : d.size });
    });
});
2020-07-27