Javascript 数据结构类型
我有多个包含子问题的问题。我想存储在一个数据结构中,这样当用户选择第一个问题时,我可以选择子问题。此外,一些子问题使用该类别内的一般问题。起初我考虑使用多维数组,但后来我意识到在数组中搜索需要相当长的时间 如有任何建议,我们将不胜感激 多谢各位 到目前为止,这是我的解决方案Javascript 数据结构类型,javascript,algorithm,data-structures,Javascript,Algorithm,Data Structures,我有多个包含子问题的问题。我想存储在一个数据结构中,这样当用户选择第一个问题时,我可以选择子问题。此外,一些子问题使用该类别内的一般问题。起初我考虑使用多维数组,但后来我意识到在数组中搜索需要相当长的时间 如有任何建议,我们将不胜感激 多谢各位 到目前为止,这是我的解决方案 //Key is the question and value(object) contains all the value related to the question categoryToSubquestions[2]
//Key is the question and value(object) contains all the value related to the question
categoryToSubquestions[2] = {"What type of countertop?":{
"stone_slab_countertops": "Stone slab countertops",
"granite_countertops" : "Granite countertops",
"marble_countertops" : "Marble countertops",
"quartz_countertops" : "Quartz countertops",
"slate_countertops" : "Slate countertops",
"solid_surface_countertops" : "Solid Surface countertops",
"concrete_countertops" : "Concrete countertops",
"corian_countertops" : "Corian countertops",
"formica_countertops" : "Formica countertops",
"stainless_countertops" : "Stainless countertops",
"wood_or_butcher_block_countertops" : "Wood or Butcher block countertops",
"laminate_countertops" : "Laminate countertops",
"selectKey":"MappedCategory"
},
"What best describes your countertop project?":{
"install_or_replace": "Install or Replace",
"repair" : "Repair",
"selectKey":"describe_countertop_project"
},
"generalQuestions2": "4"
};
//This is general question that other categories might use...It is being used in the above category
generalQuestion[4] = {"Is this project part of a larger remodel?":{
"true" : "Yes",
"false": "No",
"selectKey":"part_of_larger_remodel"
}
};
//THIS IS categoryToSuquestion index to value assosciation...(JUST TO KEEP TRACK)
var keyValue = new Array(
/*0*/ "cabinets_reface",
/*1*/ "cabinets_refinish",
/*2*/ "cabinets_countertop_install");
我现在有70个这样的问题,我不担心一旦我继续添加更多的问题,它会不会变慢?我会创建这样的数据结构。
不要忘记,您可以像访问哈希表/字典一样访问它的属性
var问题\u数据\u结构={
“Q1”:{
“问题文本”:“家长问题1?”,
“子问题”:[“子问题1”、“子问题3”]
},
“子项1”:{
“问题案文”:“子问题1”,
“家长问题”:“Q1”
},
“子项3”:{
“问题案文”:“子问题3”,
“家长问题”:“Q1”
},
“问题8”:{
“问题文本”:“没有儿童问题的常规问题?”
}
}
q=问题\数据\结构[“Q1”];
警报(问题文本);
如果(q.sub_问题和q.sub_问题长度>0){
警惕(“我有孩子问题”);
变量i=0,len=q.sub_questions.length,childQuestionObj;
对于(;i
因此,如果将数据结构的键值绑定到HTML控件ID,则可以执行类似的操作
//包含jQuery库
$。每个(问题\数据\结构、功能(问题\ id、值){
如果(value.sub_问题和&value.sub_问题长度>0){
$(“#”+问题id)。单击(函数(e){
/*显示或隐藏子问题控件*/
});
}
});
对我来说就像一棵树。提供数据结构的示例可能会有所帮助。我不知道你所说的“某些子问题使用该类别中的一般问题”是什么意思。这都是在客户端(javascript)上发生的,还是在服务器端从数据库中执行的?尝试使用映射{'question':{'question':'question':'question here','subquestions':{…}
我使用了相同的结构,但我想知道它是否会显著降低网站的速度。目前我有70个问题,还有更多的问题。DOM插入非常快(在iPhone上插入10000个div最多需要0.5秒)。所以别担心。这完全取决于处理数据结构的算法。我编写了包含嵌套子节点和近200个或更多节点的更复杂的数据结构,并且没有遇到性能问题。