javascript adding value to an json object in a loop - Stack Overflow

admin2025-04-17  0

I'm adding a value to each object in the object list. But I don't know why its adding the date for every object in every loop.

this is my code:

var emptyday: {
    "date":""
}

//make list of days in month
var monthlist = [];
for (i=0;i<=days_in_months;i++) {
    monthlist[i] = emptyday;
}

So in my example lets say that days_in_months is 31 (days)

Now es the adding

for (x=1;x<=days_in_months;x++) {

    console.log(x);
    if (x<10) {
        daynumber = "0" + x;
    } else {
        daynumber = x;
    }

    datestring = year + "-"+ (month+1) + "-" + daynumber;

    dayofmonth = monthlist[x]; 
    dayofmonth["date"] = datestring;
            //monthlist[x].date = datestring;

}

When I try adding (dayofmonth["date"] = datestring or monthlist[x].date) it adds to all date values of all objects in every loop.

The console.log looks like this for the first loop:

[Object { date= "2013-1-01"}, Object { date= "2013-1-01"}, Object { date= "2013-1-01"}, Object { date= "2013-1-01"}, Object { date= "2013-1-01"}, Object { date= "2013-1-01"}, etc

for 31 times in the first loop

And in the last loop it will be 2013-1-31

I don't understand why it is adding that value to all objects. I have tried console.log and debugging all over the place to read out values and trying to understand what goes wrong, but still haven't found a solution

I'm adding a value to each object in the object list. But I don't know why its adding the date for every object in every loop.

this is my code:

var emptyday: {
    "date":""
}

//make list of days in month
var monthlist = [];
for (i=0;i<=days_in_months;i++) {
    monthlist[i] = emptyday;
}

So in my example lets say that days_in_months is 31 (days)

Now es the adding

for (x=1;x<=days_in_months;x++) {

    console.log(x);
    if (x<10) {
        daynumber = "0" + x;
    } else {
        daynumber = x;
    }

    datestring = year + "-"+ (month+1) + "-" + daynumber;

    dayofmonth = monthlist[x]; 
    dayofmonth["date"] = datestring;
            //monthlist[x].date = datestring;

}

When I try adding (dayofmonth["date"] = datestring or monthlist[x].date) it adds to all date values of all objects in every loop.

The console.log looks like this for the first loop:

[Object { date= "2013-1-01"}, Object { date= "2013-1-01"}, Object { date= "2013-1-01"}, Object { date= "2013-1-01"}, Object { date= "2013-1-01"}, Object { date= "2013-1-01"}, etc

for 31 times in the first loop

And in the last loop it will be 2013-1-31

I don't understand why it is adding that value to all objects. I have tried console.log and debugging all over the place to read out values and trying to understand what goes wrong, but still haven't found a solution

Share edited Jan 4, 2013 at 13:36 oleq 15.9k1 gold badge39 silver badges69 bronze badges asked Jan 4, 2013 at 13:33 LokkioLokkio 1,7133 gold badges16 silver badges19 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 4

The references in your array all point to the same object. Javascript is pass by value. So when you do

//make list of days in month
var monthlist = [];
for (i=0;i<=days_in_months;i++) {
    monthlist[i] = emptyday;
}

the you are putting a copy of the reference emptyday at every position in the array. Since the copies of the reference all point to the same object literal, you have an array of references to one object.

You need to create a new object literal every time thru the list.

var monthlist = [];
for (i=0;i<=days_in_months;i++) {
    monthlist[i] = {
       date: ""
    };
}
转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1744884596a272461.html

最新回复(0)