For Loop | Object keeps its value?

Meloper
Kilo Sage

Hello,
below you can see 2 scripts, the only difference is the setting of the variable "release_year_obj" once outside and once inside the For loop.

Why in the first example, is always only Q4/2022 pushed into the array?
The logs before show that there should be a different quarter as value in the object key quartal per loop.

I hope you understand my question

 

function getReleases() {

    var releases = [];
    var year = '2022';
    var quartal = '';
    var release_year_obj = {};


    for (var i = 1; i < 5; i++) {
		

        if (i == 1) {
         
            quartal = "Q" + i + "/" + year;

        } else if (i == 2) {
            quartal = "Q" + i + "/" + year;


        } else if (i == 3) {
            quartal = "Q" + i + "/" + year;


        } else {
            quartal = "Q" + i + "/" + year;

        }
		
        release_year_obj.quartal = quartal;
		
		gs.log("quartal: " +  release_year_obj.quartal)
        releases.push(release_year_obj);
    }
    return releases;
}

gs.log("Object: " + JSON.stringify(getReleases()));

 

find_real_file.png

 

--------------------------------------------------

 

function getReleases() {

    var releases = [];
    var year = '2022';
    var quartal = '';
    


    for (var i = 1; i < 5; i++) {
		var release_year_obj = {};

        if (i == 1) {
         
            quartal = "Q" + i + "/" + year;

        } else if (i == 2) {
            quartal = "Q" + i + "/" + year;


        } else if (i == 3) {
            quartal = "Q" + i + "/" + year;


        } else {
            quartal = "Q" + i + "/" + year;

        }
		
        release_year_obj.quartal = quartal;
		
		gs.log("quartal: " +  release_year_obj.quartal)
        releases.push(release_year_obj);
    }
    return releases;
}

gs.log("Object: " + JSON.stringify(getReleases()));

find_real_file.png

2 REPLIES 2

Anil Lande
Kilo Patron

Hi,

Not sure if my logic is correct or not.

In first case you have declared an object release_year_obj outside for loop, that means it was initialized once and space was allocated to it once.

In this case value is stored to same address (value was overwritten while preparing response)and that address was referred while returning data from function. 

In second case you have declared an object release_year_obj inside for loop, and it is being initialized in every iteration and space was allocated 4 times. So at the time of returning four different addresses were referred and it returned correct data. 

 

Not sure how JavaScript engine is processing but this.

In first case if you do it like below then it will store actual value in array instead of address and you will get all four quarters:

releases.push(JSON.stringify(release_year_obj));

 

Thanks,
Anil Lande

Please appreciate the efforts of community contributors by marking appropriate response as correct answer and helpful, this may help other community users to follow correct solution in future.
Thanks
Anil Lande

Thanks for your answer and the time spent on it