0

如何在深度 = 1 的对象树中返回给定节点(键名)的子节点,仅表示第一个子节点。

具体问题:

所以这是一个示例数据对象......

{
    "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"
            }
        }
    }
}; 

对象的每一级代表页面上的一个下拉菜单。

我需要做的是,当有人从下拉菜单之一中选择某些内容时,我需要返回所有剩余菜单的可能性..

例如,假设我从下拉列表 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

到目前为止,我已经能够实现这么多......当这种情况发生某种组合时,问题就出现了:

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

这是一个示例对象,我所有的对象都将具有不同的深度和复杂性......我已经做了很多尝试,希望有人可以向我展示光线:)

谢谢

4

1 回答 1

0

想一想,你想要基于选择(节点名称)它的孩子,所以你不需要完整的对象树(虽然你可以使用它),你可以更容易地使用邻接表作为查找表(哈希表) :

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

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

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

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

所以现在有人选择“1753”,你查找 adj[“1753”] 并得到可能的孩子的数组,与其他人一样。

但是如果你想使用你不知道深度的当前数据结构,那么你应该使用 for(var key in tree) + recursion 来遍历树,但这几乎没有意义,因为你需要一个给定的(父) 节点名称遍历整个树以“知道”父节点所在的位置(假设唯一键!)。

所以最好使用邻接列表。

于 2012-04-16T03:41:03.130 回答