1
var mypafgfr = [];
mypafgfr[0] = "tt";
mypafgfr[1] = "tt";
mypafgfr[2] = "tt";
var outputdata = {};

function ls() {
    console.log(mypafgfr[0]); {
        var gg = {};
        gg["n"] = mypafgfr[0];
        gg["image1"] = mypafgfr[1];
        gg["image2"] = mypafgfr[2];
        outputdata["folder"] = gg;
    }
    console.log(outputdata);
}

ls();

mypafgfr[0] = "yy";
mypafgfr[1] = "yy";
mypafgfr[2] = "yy";

ls();

In console I'm expect to see

Object { n="tt",  image1="tt",  image2="tt"}
Object { n="yy",  image1="yy",  image2="yy"}

but I got

Object { n="yy",  image1="yy",  image2="yy"}
Object { n="yy",  image1="yy",  image2="yy"}

What I'm doing wrong? How it happen?

Ousmane D.
  • 54,915
  • 8
  • 91
  • 126
Slava32768
  • 49
  • 1
  • 6

1 Answers1

-1

That is because console.log() prints the reference of the object.

You could you console.dir() instead to see how the object looks at call time.

One more fallback you can use is to use JSON.stringify() at the time of logging it, which does not point to the reference.

console.log(JSON.stringify(outputdata));
canon
  • 40,609
  • 10
  • 73
  • 97
Sushanth --
  • 55,259
  • 9
  • 66
  • 105