Is it possible in JS to know where a reference is in its hierarchy?

32 Views Asked by At

I have a case where I want to mark my 'posts' (like articles) with the country they belong to, while allowing to go a scope higher: example france < europe < world

This is how I structured my hierarchy: world.js

export default {
    world: {
        europe: {
            albania: 'albania',
            austria: 'austria',
            belgium: 'belgium',
            bulgaria: 'bulgaria',
            croatia: 'croatia',
            czech: 'czech',
            //...
            france: 'france'
    }
}

It is not necessary to have this structure. But what I want to achieve is that each 'post' I have would contain a location propoerty with a value like ['world', 'europe', 'france']. My pain is how to extract from the value 'france' its hierarchy ['world', 'europe', 'france'] without having to scan all the tree: I need kind of a reference to the property france to know to which 'context' it belongs to and navigate the tree in a child to parent way to obtain france (parent: europe (parent: world)) (not searching from the root where france might be in the structure)

Of course I can declare a verbose tree explicitly specifying the parent each time, but my question is about an elegant way of doing this

0

There are 0 best solutions below