我有多个问题,其中包含子问题。我想存储在数据结构中,以便在用户选择第一个问题时我可以选择子问题。此外,一些子问题使用该类别中的一般问题。最初我想过使用多维数组但后来我意识到搜索数组需要相当长的时间。
任何建议都表示赞赏。
谢谢
到目前为止,这是我的解决方案。
//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个,我担心如果我继续添加更多问题会减慢它吗?
答案 0 :(得分:0)
我会创建一个这样的数据结构 不要忘记你可以像它的属性上的Hashtable / Dictionary一样访问它。
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控件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 */
});
}
});