Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
475 views
in Technique[技术] by (71.8m points)

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

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.

D3 JSON DATA CONVERSION

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.

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!!<


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Updated to use a recursive method

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

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 });
    });
});

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...