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

后端 未结 2 1334
轻奢々
轻奢々 2020-12-02 17:35

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.

相关标签:
2条回答
  • 2020-12-02 18:25
    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);
    
    0 讨论(0)
  • 2020-12-02 18:34

    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 });
        });
    });
    
    0 讨论(0)
提交回复
热议问题