简体   繁体   中英

Get children of a given node within an object tree

How do I return the children of a given node (key name) within an object tree with depth =1, means only the first children.

Specific issue:

So here is a sample data object...

{
    "1753": {
        "1755": {
            "1758": {
                "1762": "1753_1755_1758_1762",
                "1760": "1753_1755_1758_1760",
                "1764": "1753_1755_1758_1764",
                "1761": "1753_1755_1758_1761"
            },
            "1759": {
                "1762": "1753_1755_1759_1762",
                "1760": "1753_1755_1759_1760",
                "1764": "1753_1755_1759_1764",
                "1761": "1753_1755_1759_1761"
            }
        },
        "1756": {
            "1758": {
                "1762": "1753_1756_1758_1762",
                "1760": "1753_1756_1758_1760",
                "1764": "1753_1756_1758_1764",
                "1761": "1753_1756_1758_1761"
            },
            "1759": {
                "1762": "1753_1756_1759_1762",
                "1760": "1753_1756_1759_1760",
                "1764": "1753_1756_1759_1764",
                "1761": "1753_1756_1759_1761"
            }
        },
        "1757": {
            "1758": {
                "1762": "1753_1757_1758_1762",
                "1760": "1753_1757_1758_1760",
                "1764": "1753_1757_1758_1764",
                "1761": "1753_1757_1758_1761"
            },
            "1759": {
                "1762": "1753_1757_1759_1762",
                "1760": "1753_1757_1759_1760",
                "1764": "1753_1757_1759_1764",
                "1761": "1753_1757_1759_1761"
            }
        }
    },
    "1754": {
        "1755": {
            "1758": {
                "1763": "1754_1755_1758_1763"
            }
        },
        "1756": {
            "1758": {
                "1763": "1754_1756_1758_1763"
            }
        },
        "1757": {
            "1758": {
                "1763": "1754_1757_1758_1763"
            }
        }
    }
}; 

Each level of the object represents a drop down menu on a page.

What i need to do is when someone selects something from one of the drop down menus I need to return all of the remaining menu's possibilities..

SO for example lets say I select value "1758" from dropdown-3 (because it is the third level in the object), do the following:

I would need to return info stating that because I selected "1758" from dropwdown-3... 
    -dropdown-1 can be 1753, 1754.
    -dropdown-2 can be 1755,1756,1757
    -dropdown-3 can remain unchanged
    -dropdown-4 can be 1762,1760,1764,1761,1763

So far I have been able to achieve that much... the problem comes in when some combination of this happens:

I have selected option "1758" from dropdown-3, AND THEN SELECTED "1754" from dropdown-1...
    -dropdown-1 has the most recent selected value, it can remain unchanged
    -dropdown-2 can be 1755,1756,1757
    -dropdown-3 is set, but can still have its possible values modified & checked for accuracy
        -dropdown-3 can be only 1758
    -dropdown-4 can be only 1753

This is a sample object, all of my objects will have varying depth & complexity... I've made quite a few attempts at this, hopefully someone can show me the light :)

Thanks

Think about it, you want based on the selection (node name) its children, so you don't need the full object tree (although you could use it), you can easier use a adjacency list as a lookup table (hash table):

adj["1753"] = ["1755"];

adj["1755"] = ["1758"]

adj["1758"] = ["1762", "1760", "1764", "1761"]

adj["1762"] = "1753_1755_1758_1762"    
...

So somebody now selects "1753", you look up adj["1753"] and get the array with the possible chidlren, same with the others.

But if you want to use your current data structure which depth you don't know, then you should use for(var key in tree) + recursion to traverse the tree, but this is pretty much pointless because you need for a given (parent) node name traverse the whole tree to "know" where the parent is located (assuming unique keys!).

So better use adjacency lists.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM