如何在Javascript中表示此数据结构

How to represent this data structure in Javascript

本文关键字:表示 数据结构 Javascript      更新时间:2023-09-26

谁能告诉我哪个数据结构在Javascript中代表以下场景。帮助感激。我正在寻找不同的选项,如树遍历,双重链表,但需要更好的输入。

用例:来自服务器的对象表示如下

behaviour Id 1 [checkbox]
Combinable is [2,3]
Behaviour 2 [checkbox]
combinable is [8]
Behaviour 3 [checkbox]
combinable is [5]
Behaviour 4 [checkbox]
combinable is [7]
Behaviour 5 [checkbox]
combinable is []
Behaviour 6 [checkbox]
combinable is []
Behaviour 7 [checkbox]
combinable is []
Behaviour 8 [checkbox]
combinable is [9]
Behaviour 9 [checkbox]
combinable is []

考虑以下场景,我需要双向遍历。

用例1:当用户选择行为id为1时,[1,2,8,9]和[1,3,5]应该启用。其余的复选框将被禁用。

用例2:这是双向的。继续用例1,当用户选择8并取消选择id 1时,则[8,9]应该启用,但[1,2,3,4,5,6]应该禁用。

我编辑了这个问题,以确保它更容易理解…谢谢。

为什么不使用json结构来存储关系呢?

var relationship = {
   1 : {
     2,3        
   },
   2 : {
    4  
   },
   3 : {
    5,6
   },
   4 : {
    6,7
   }
}

因此,将行为id与关系对象中的键绑定。迭代并勾选相应的值