Randy Bishop
Randy Bishop

Reputation: 35

Pushing object to array results in same value

I have the following javascript code that does not work as I would expect it to. I have a list of checkboxes of which two of the items are "TestDuration" and "AssessmentScores". I'm trying to iterate through the list (which works fine) and have it add the values that are checked to the array.

var SAIndex = 0;
var SSIndex = 0;
var ScoresIndex = 0;
var SubAssessments = [];
var SubAssessmentScores = [];

//Get to the container element
var SSList = document.getElementById("islSubAssessmentScore_container");  

//turn it into an array of the checkbox inputs
SSList = SSList.getElementsByTagName("input"); 

//create a temporary object to store my values
var tempPair = new Object(); 

//iterate through the checkbox lists
for(var i = 1; i < SSList.length;i++) 
{
    //if the value is checked add it to the array
    if (SSList[i].checked) 
    {
        var P = SubAssessments[SAIndex];
        var V = SSList[i].value;
        //tempPair.Parent = SubAssessments[SAIndex];
        tempPair.Parent = P;
        //tempPair.Value = SSList[i].value;
        tempPair.Value = V;
        //show me the values as they exist on the page
        alert(tempPair.Parent + "|" + tempPair.Value); 
        SubAssessmentScores.push(tempPair);

        //show me the values I just added to the array
        alert(SubAssessmentScores.length-1 + "|" + SubAssessmentScores[SubAssessmentScores.length-1].Parent + "|" + SubAssessmentScores[SubAssessmentScores.length-1].Value); 

        //uncheck the values so when I refresh that section of the page the list is empty
        SSList[i].checked = false; 
    }
}

//output the list of objects I just created
for (i = 0;i < SubAssessmentScores.length;i++) 
    alert(i + "|" + SubAssessmentScores[i].Parent + "|" + SubAssessmentScores[i].Value)

Now what happens is that when I iterate through the list I get the following alerts:
-first pass-

StudentID|TestDuration
0|StudentID|TestDuration

-second pass-

StudentID|AssessmentScores
1|StudentID|AssessmentScores

This is what I expect to output... However at the end of the code snippet when it runs the for loops to spit out all the values I get the following alerts...

0|StudentID|AssessmentScores
1|StudentID|AssessmentScores

I can't for the life of me figure out why it's replacing the first value with the second value. I thought it might be using a reference variable which is why I added in the P and V variables to try to get around that if that was the case, but the results are the same.

Upvotes: 0

Views: 2912

Answers (1)

Roger
Roger

Reputation: 2952

This is because you are adding the same variable every iteration of the loop.

Try changing your push like this:

SubAssessmentScores.push({
  Parent: P,
  Value: V
});

That said, I recommend you study a little more javascript and conventions in the language, for example your variable naming is frowned upon because you should only use capital letters on the beginning of a name for constructor functions.

A good book is Javascript the good parts by Douglas Crockford.

Upvotes: 2

Related Questions