'Creating hierarchy using id and parentid

Possible duplicate, but this one unlike other questions does not wish to hook objects to another. I would want to create a new objects/array containing the hierarchy data

So I have these datasets:

var sets = {
    'dataSet1': {
        'ID': '1',
        'ParentID': '0'
    },

    'dataSet2': {
        'ID': '2',
        'ParentID': '1'
    },

    'dataSet3': {
        'ID': '3',
        'ParentID': '1'
    },

     'dataSet4': {
         'ID': '4',
         'ParentID': '3'
    }
}

I want to loop through them and dynamically create a hierarchy by using ID and ParentID. The result of the process I would like to output into something like:

var tiers = {
     'tier1':[
     {
        'parentid':'0',
        'id':'1'
     }
     ]

    ,'tier2':[
     {
        'parentid':'1',
        'id':'2'
     },
     {
        'parentid':'1',
        'id':'3'
     }
     ]
    ,'tier3':[
     {
        'parentid':'3',
        'id':'4'
     }
     ]
}

Basically the tier object will hold tier arrays containing objects that have the id and the parent id fields.

So when I access the data all I have to do is get them via tiers['tier2'][1]. All I need to do is structure the data into a hierarchy. Note: Native js

This is my current attempt, but this only works for the one in the top of the hierarchy, the second tier and below are the biggest problem

var datasetobj = sets;
var hierarchy = tiers;
for (x in datasetobj) {

        if (datasetobj[x]['ParentID'] == 0) {
            hierarchy['tier1'].push({
                'parentid': datasetobj[x]['ParentID'],
                'id': datasetobj[x]['ID']
            });
        }

    }

Now this is fairly easy if I'm going to use static approaches by limiting the number of tiers the program can handle, and then checking each ID then inserting tiers. But that's not exactly the OOP approach of doing things.



Solution 1:[1]

The problem with your if condition in your for loop. It only proccess the top level element if (datasetobj[x]['ParentID'] == 0) only processes parent id 0.

To get what you're after dynamically create hierarchy arrays and remove the if condition. Do something like:

      var tiers = {};
      var sets = {
        'dataSet1': {
            'ID': '1',
            'ParentID': '0'
        },
    
        'dataSet2': {
            'ID': '2',
            'ParentID': '1'
        },
    
        'dataSet3': {
            'ID': '3',
            'ParentID': '1'
        },
    
         'dataSet4': {
             'ID': '4',
             'ParentID': '3'
        }
    };
    var datasetobj = sets;
    var hierarchy = tiers;
    for (x in datasetobj) {
            if (datasetobj[x]['ParentID']) { 
                var idx = 'tier'+datasetobj[x]['ParentID'];
                if(!hierarchy[idx])
                    hierarchy[idx]=[];
                hierarchy[idx].push({
                    'parentid': datasetobj[x]['ParentID'],
                    'id': datasetobj[x]['ID']
                });
            }
        };
    console.log(tiers);

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Nishanth Matha