数据结构的类型
Type of datastructure
我有多个包含子问题的问题。我想存储在一个数据结构中,这样我就可以在用户选择第一个问题时选择子问题。此外,有些子问题在该类别中使用一般问题。最初我想使用多维数组,但后来我意识到它将花费相当长的时间来搜索数组。
欢迎提出任何建议。
谢谢
这是我到目前为止的解决方案。
//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 questions_data_structure = {
"Q1": {
"question_text": "Parent Question #1?",
"sub_questions": ["SubQues1", "SubQues3"]
},
"SubQues1": {
"question_text": "Sub Question 1?",
"parent_question": "Q1"
},
"SubQues3": {
"question_text": "Sub Question 3?",
"parent_question": "Q1"
},
"Ques8": {
"question_text": "Regular question without children questions?"
}
}
q = questions_data_structure["Q1"];
alert(q.question_text);
if (q.sub_questions && q.sub_questions.length > 0) {
alert("I have child questions");
var i = 0, len = q.sub_questions.length, childQuestionObj;
for (; i < len; i++) {
childQuestionObj = questions_data_structure[q.sub_questions[i]];
alert(childQuestionObj.question_text);
}
}
else {
alert("I DON'T have child questions");
}
因此,如果您将数据结构的KEY值绑定到HTML Control id,您可以这样做。// include jQuery library
$.each(questions_data_structure, function(question_id, value) {
if (value.sub_questions && value.sub_questions.length > 0) {
$('#' + question_id).click( function(e) {
/* Show or hide your child question controls */
});
}
});
相关文章:
- JS库支持各种数据结构?(如爪哇的番石榴)
- JavaScript数据结构
- Node JS,传统的数据结构?(如Set等),任何类似Java.util的node
- 更正扁平数据模型和noSQL数据结构
- 用于筛选无模式集合的最快数据结构
- 将数据结构转换为二进制数据
- 使用 ng-change 对输入类型数据进行 JavaScript 取消事件
- JavaScript 设置具有对数搜索时间的数据结构
- 更好的数据结构来处理这个数组
- Firebase 数据结构理念
- 基于其他数据结构更新 AngularJS 中的数据结构
- JavaScript - JSON 数据结构的构建 - 如何使用变量值更改键名
- 如何处理在javascript中访问数据结构的两个回调
- 文字与原型对象表示法的数据结构
- 表示可用产品的所有组合的数据结构
- 在Flux体系结构中,如何管理存储相同类型数据的存储
- Immutable.js:表示2D游戏场的数据结构
- javascript和python返回的相同数据结构在d3.js中表现不同
- 无法识别的数据结构-转换为对象
- 数据结构的类型