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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19317115/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 23:40:39  来源:igfitidea点击:

Convert flat JSON file to hierarchical json data like flare.json [d3 example file]

javascriptjqueryjsond3.js

提问by Unknown User

After a troublesome fight i almost figured how to convert a flat json file to a Hierarchical one. I didn't write the function by my own. I copied it from the below post.

经过一场麻烦的战斗,我几乎想出了如何将平面 json 文件转换为分层文件的方法。函数不是我自己写的。我从下面的帖子中复制了它。

D3 JSON DATA CONVERSION

D3 JSON 数据转换

But now the problem now is, the function which was written in the post has just 2 levels of hierarchy. But i'm looking for 4 levels hierarchy. I tried to override the function where i failed but.

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

Code with what i'm trying.

用我正在尝试的代码编写代码。

    
         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, '  '));
        

Output of the current code

当前代码的输出

    {
      "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"
            }
          ]
        }
      ]
    }

Desired Output Format:

所需的输出格式:

    {
      "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"
                }
              ]
            },
          ]
        }
      ]
    }

I'm cracking my head from a week but i alone couldn't figure it out. Someone please amend the function to get the data in the hierarchical format as i've updated.

我从一个星期开始就崩溃了,但我一个人无法弄清楚。有人请修改函数以在我更新时以分层格式获取数据。

Thanks in advance!!

提前致谢!!

回答by Richard Marr

Updated to use a recursive method

更新为使用递归方法

This should work for nlevels rather than just 2 or 3. You just need to specify which properties define which levels.

这应该适用于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 });
    });
});

回答by Lalit Tyagi

var data = [
    { "state": "UP", "district": "Agra", "block": "IradatNagar","school":"APS","name":"A" },
    { "state": "UP", "district": "Agra", "block": "IradatNagar","school":"IPS","name":"B" },
    { "state": "UP", "district": "Agra", "block": "IradatNagar","school":"APS","name":"C" },
    { "state": "MP", "district": "Bhopal", "block": "chota_Bhopal","school":"DPS","name":"D" },
    { "state": "UP", "district": "Mathura", "block": "Farah","school":"HPS","name":"E" },
    { "state": "UP", "district": "Kanpur", "block": "Mania","school":"BPs","name":"F" },
    { "state": "UP", "district": "Agra", "block": "Arjun Nagar","school":"GPS","name":"G" }, 
    { "state": "MP", "district": "Gwalior", "block": "Surya Nagar","school":"DPS","name":"H" }
];

var newData = { name :"State", children : [] },
    levels = ["state","district","block","school"];

data.forEach(function(d){
    var depthCursor = newData.children;
    levels.forEach(function( property, depth )
    {
        var index;
        depthCursor.forEach(function(child,i)
        {
            if ( d[property] == child.name ) 
                index = i;
        });

        if ( isNaN(index) ) 
        {
            depthCursor.push({name : d[property], children : []});
            index = depthCursor.length - 1;
        }

        depthCursor = depthCursor[index].children;

        if ( depth === levels.length - 1 )
        {
            depthCursor.push({ name : d.name});
        }
    });
});

console.log(newData);