简体   繁体   English

获取对象树内给定节点的子代

[英]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. 如何返回深度为= 1的对象树中给定节点(键名称)的子代,意味着仅第一个子代。

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: 例如,假设我从下拉列表3中选择值“ 1758”(因为它是对象中的第三级),请执行以下操作:

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. 因此,现在有人选择“ 1753”,您查找adj [“ 1753”]并获得包含可能字符的数组,与其他数组相同。

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!). 但是,如果您想使用当前深度未知的数据结构,则应使用for(树中的var键)+递归遍历树,但这几乎没有意义,因为您需要给定的(父)节点名称遍历整个树,以“知道”父节点所在的位置(假设有唯一键!)。

So better use adjacency lists. 因此,最好使用邻接表。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在具有多个子节点的树中打印从根到给定节点的路径 - Print path from root to a given node in a tree with multiple children 在具有任意数量孩子的树中,查找节点是在给定节点的左半子树还是右半子树中 - In a tree with arbitrary number of children, find whether a node is in the left half subtrees or right half subtrees of a given node Angular treetable(Primeng 树表)——获取所有子节点的父节点 - Angular treetable (Primeng tree table) - get all the parent node of the chIldren Javascript递归函数获取给定节点的所有子节点 - Javascript recursive function to get all children for given node 节点对象树如何进入浏览器 - How does the node object tree get in the browser 如何从树对象获取给定路径的子树? - How to get a subtree given a path from a tree object? extjs 3.4无法访问树中节点的子级 - extjs 3.4 unable to access children of a node in a tree ExtJS 4.2.1,Tree,将子节点添加到节点 - ExtJS 4.2.1 , Tree, adding children to a node 如何获取树的每个节点的父母和所有孩子的数组? - How can I get an array of the parents and all of the children of each node of a tree? Fancytree JQuery-获取节点子级和子级子级 - Fancytree JQuery - Get node children and sub children
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM