copy - JavaScript stop referencing object after pass it to a function -
i know javascript passes objects reference , i'm having lot of trouble following code:
function dogradeassignmentcontent(dtos) { var x = 5; var allpages = []; var stage = new app.ui.popupdisplay.popupstageassignmentgrader(null, that);// pass launch element for(var = 0; < dtos[0].result.students.length; ++i) { var pagesset = []; for(var j = 0; j < dtos[0].result.questions.length; ++j) { var questionobject = jquery.extend(true, {}, new object()); questionobject = dtos[0].result.questions[j]; if(dtos[0].result.students[i].answers[j].assignmentquestionid === questionobject.questionid) {// expected, if not here wrong questionobject.answer = dtos[0].result.students[i].answers[j].studentanswer; questionobject.pointsreceived = dtos[0].result.students[i].answers[j].pointsreceived; } else { var theanswer = findassociatedstudentanswer(questionobject.questionid, dtos[0].result.students[i].answers[j]); if(theanswer !== null) { questionobject.answer = theanswer.studentanswer; questionobject.pointsreceived = theanswer.pointsreceived; } else { alert("unexpected error. please refresh , try again."); } } pagesset[pagesset.length] = new app.ui.popupdisplay.stageassignmentgradingpages[dtos[0].result.questions[j].questiontype.charat(0).touppercase() + dtos[0].result.questions[j].questiontype.slice(1) + "questionassignmentgradingpage"](j + 1, questionobject); } var studentinfo = {}; studentinfo.avatar = dtos[0].result.students[i].avatar; studentinfo.displayname = dtos[0].result.students[i].displayname; stage.addpageset(pagesset, studentinfo); } stage.launch(); }
first let me show result (dtos) looks can better understand how function parsing it:
the result (dtos) object , looks like:
- dtos array
- dtos[0], static here
- dtos[0].result, static here
- dtos[0].questions array
- dtos[0].questions.index0 - indexn. describes our questions, each 1 object
- dtos[0].students array
- dtos[0].students[0]-[n].answers array. each student array/object has answers array. each student have many elements in answers array there questions in dtos[0].questions. each element object
now in here create object stage. important things here has array called "this.studentspages". array have many entries there students in dtos[0].students.
so loop through loop disecting dtos array , creating pagesset array. here comes problem. on first iteration through loop create questionobject element. have tried doing var questionobject = {}, see attempt fix problem seeing, didn't work either.
so @ end of first iteration of outer loop call stage.addpageset, happens here:
var pageobject = []; pageobject["questions"] = pageset; pageobject["displayname"] = studentinfo.displayname; this.studentspages[this.studentspages.length] = pageobject; if(this.studentspages.length === 1) {// first time for(var = 0; < pageset.length; ++i) { this.addpage(pageset[i]); } }
the important thing take notice of here add pageobject on this.studentspages empty array before first call. pageobject has pageset plus little bit more information. remember, pageset object , passed reference.
on next iteration of loop, when hit line:
questionobject.answer = dtos[0].result.students[i].answers[j].studentanswer;
it goes wrong. changes local copy of questionobject, changes copy of questionobjec passed addpageset , added studentspages array in first iteration. so, if had 2 students coming in, when said , done, studentspages hold 2 identical objects. should not true.
the problem questionobject in dogradeassignmentcontent function keeping reference object created on previous iteration , overrides on subsequent iterations.
what can fix this?
thanks help!
with out having looked @ closely believe need change following:
// before: var questionobject = jquery.extend(true, {}, new object()); questionobject = dtos[0].result.questions[j]; // after: var questionobject = jquery.extend(true, {}, dtos[0].result.questions[j]);
i didn't closely if there other instances in code needs applied, core concept utilize jquery's deep copy generate duplicate of object not wish retain reference to.
Comments
Post a Comment