Home > Blockchain >  Find the top hierarchy elements from array with parent child relationship
Find the top hierarchy elements from array with parent child relationship

Time:10-25

I am trying to figure out a way to filter out top hierarchical elements from an array, where each object may have a parent-child hierarchy with other elements of the array. Here is what the array looks like:

[
    {
        "Code": "1A",
        "name": "Western Europe",
        "hierarchy": "World/",
    },
    {
        "Code": "AT",
        "name": "Austria",
        "hierarchy": "World/ Western Europe/"
    },
    {
        "Code": "NL",
        "name": "Netherlands",
        "hierarchy": "World/ Western Europe/"
    }
]

For this example, the output should be only the first object, because Western Europe is the parent of Austria and the Netherlands.

[
    {
        "Code": "WO",
        "name": "World",
        "hierarchy": "",
    },
     {
        "Code": "NA",
        "name": "North America",
        "hierarchy": "World/",
    },
    {
        "Code": "NL",
        "name": "Netherlands",
        "hierarchy": "World/ Western Europe/"
    }
]

Here the output will be World because World is the top Hierarchy.

[
    {
        "Code": "1A",
        "name": "Western Europe",
        "hierarchy": "World/",
    },
     {
        "Code": "NA",
        "name": "North America",
        "hierarchy": "World/",
    },
    {
        "Code": "US",
        "name": "United States",
        "hierarchy": "World/ North America/",
    },
    {
        "Code": "NL",
        "name": "Netherlands",
        "hierarchy": "World/ Western Europe/"
    },
    {
        "Code": "RS",
        "name": "Russia",
        "hierarchy": "World/ Eastern Europe/",
    },
]

Here, the output should be Western Europe, North America, and Russia (As we don't have Eastern Europe in the list so Russia is at the top of its hierarchy). So, We have to get the top hierarchical elements. I have a function where I can pass the Code and get the hierarchy structure:

getParentChildHierarchy(string Code) { //some code here }

Input: AT
Output: WO>1A>AT

Can you suggest an optimum way to solve this?

CodePudding user response:

One way is to create a Set of all the paths in the data set, i.e. the concatenation of hierarchy with the name (following the same format with slashes and spacing). Then check for which nodes the hierarchy (which references a parent) actually exists in that set. If not, it does not have a parent in the data set:

const data = [
    {
        "Code": "1A",
        "name": "Western Europe",
        "hierarchy": "World/",
    }, {
        "Code": "NA",
        "name": "North America",
        "hierarchy": "World/",
    }, {
        "Code": "US",
        "name": "United States",
        "hierarchy": "World/ North America/",
    }, {
        "Code": "NL",
        "name": "Netherlands",
        "hierarchy": "World/ Western Europe/"
    }, {
        "Code": "RS",
        "name": "Russia",
        "hierarchy": "World/ Eastern Europe/",
    },
];

const keys = new Set(data.map(o => o.hierarchy   " "   o.name   "/"));
const topLevel = data.filter(o => !keys.has(o.hierarchy));
console.log(topLevel);

  • Related