javascript - How do you create a serialized set of objects nested within another one? -


at end of day, need javascript function me create:

object question[0]= {questiontext ,answer[{answertext1, icon1}, answertext2, icon2},.....]} 

i have question class has questions question text, , number of answers. questions in order. answers class of own, each answer has number of answers, answer text, , icon. i'm not sure if i've declared correctly, here attempt @ function create question object:

function question(qtext, a) {     this.questiontext = qtext;     this.answer = a;     } function answer(atext, icon) {     this.answertext = atext;     this.icon= icon; }  var answerforquestion1[0]= new answer {"male", maleicon} var answerforquestion1[1]= new answer {"female", femaleicon}] var question[0]= new question ("what's gender?",answerforquestion1) 

but creates unnecessary answer variables. without knowing how big answer question going be, how create 1 function can create question answers @ once?

you using var redefine variable of array each time? also, have consistently inconsistent use of invalid syntax.


anyways, thinking object-oriented. think this: "final" result trying see provides question , answers. seems appropriate make object, call qa object. way, able create instance of business-logic-specific pair. can pass these instances around & understandably.

function qa(question, answercollection) {     this.question = question;     this.answercollection = answercollection; };  qa.prototype.getquestion = function() { }; //whatever 

thinking implementing further (you've thought through, skipping first step), think of sort of object represent instance of question, , sort of object represent instance of answer (these called entities). additionally, thinking future, may want sort of collection interface (it acts data collection factory) 'create' answer collections:

/**  * entities  */  function question() {     this.question = this.icon = null; };  question.prototype.setquestion = function() { }; //whatever  function answer() {     this.answer = this.icon = null; };  answer.prototype.setanswer = function() { }; //whatever  /**  * factory  */  var answercollectionfactory = {};  answercollectionfactory.build = function(arr) {     var tmpaswer, collection = []      for(var key in arr) {         tmpaswer = new answer();         tmpaswer.setanswer(arr[key].answer);         tmpaswer.seticon(arr[key].icon);          collection.push(tmpanswer);     }      return collection; }; 

following this/a similar implementation, readable & portable code instantiates want:

var question = new question; question.setquestion('what\'s gender?'); question.seticon(); //whatever  var answercollection = answercollection.build([     {answer: ..., icon: ...},     {answer: ..., icon: ...} ]);  new qa(question, answercollection); //there ago 

at end of day: if don't need use user-defined objects & new, can go simpler object solution:

var qa = {     question: {question: ..., icon: ...},     answers: [         {answer: ..., icon: ... },         {answer: ..., icon: ... }     ] }; 

Comments

Popular posts from this blog

Spring Boot + JPA + Hibernate: Unable to locate persister -

go - Golang: panic: runtime error: invalid memory address or nil pointer dereference using bufio.Scanner -

c - double free or corruption (fasttop) -