2

I have the array NatArray and the object NatObj ,This object I am pushing to the former array ,but while retrieving its getting the last entry only , Here is my code

var tArray = [];
var tObj ;
tObj = {};
tObj.TranslatedLocIpAddr = 202116107;
tObj.TranslatedLocPort = 123;
tObj.LocIp = 50529027;
tObj.LocPort = 324;
tObj.LocPortRange = 5;
count = 0;
tArray.push(tObj) ;  

tObj.TranslatedLocIpAddr = 202116108;
tObj.TranslatedLocPort = 130;
tObj.LocIp = 67372036;
tObj.LocPort = 324;
tObj.LocPortRange = 5;
count = 1;
tArray.push(tObj) ;  

for (var i = 0; i <= count ;i++) {
    if( (tArray[i].TranslatedLocIpAddr == tGlobalIp)
     && (tArray[i].TranslatedLocPort == tGlobalPort) ) {
        alert("Existing t entry"); 
        return false;
    } 
}

I have verified the tArray[i].TranslatedLocIpAddr value, every time its retrieving the last value in the array only.

Cerbrus
  • 70,800
  • 18
  • 132
  • 147
kelvin
  • 149
  • 7

3 Answers3

6

You are changing the same object. You pushed tObj to the array, but the object is still there, and the array holds a reference to that object. So any changes will be reflected anywhere the object is referenced.

See this SO answer for more details: Javascript by reference vs. by value

Community
  • 1
  • 1
Jan Hančič
  • 53,269
  • 16
  • 95
  • 99
2

Pushing an object into an array just puts a reference to the object into the array, not a copy of the object. So both your array elements point to the exact same object which will obviously contain all the properties that you last assigned to it. If you want to fix your code, then you need to actually create a second object like this:

var tArray = [];
var tObj ;
tObj = {};
tObj.TranslatedLocIpAddr = 202116107;
tObj.TranslatedLocPort = 123;
tObj.LocIp = 50529027;
tObj.LocPort = 324;
tObj.LocPortRange = 5;
count = 0;
tArray.push(tObj) ;  

// create second object that is different from the first object
tObj = {};
tObj.TranslatedLocIpAddr = 202116108;
tObj.TranslatedLocPort = 130;
tObj.LocIp = 67372036;
tObj.LocPort = 324;
tObj.LocPortRange = 5;
count = 1;
tArray.push(tObj) ;  

for (var i = 0; i <= count ;i++) {
    if( (tArray[i].TranslatedLocIpAddr == tGlobalIp)
     && (tArray[i].TranslatedLocPort == tGlobalPort) ) {
        alert("Existing t entry"); 
        return false;
    } 
}
jfriend00
  • 683,504
  • 96
  • 985
  • 979
  • Wouldn't you need `tObj1` or something like that? – Jan Hančič Dec 18 '12 at 08:30
  • 1
    @JanHančič - no you don't need a second variable. Right after the first `.push()`, both the array element and the `tObj` variable point the same object. Then, when you assign `tObj = {};` the second time, that assigns a new empty object to `tObj`, but the first element of the array still contains a reference to the first object. The second `.push()` then puts the second object into the array. – jfriend00 Dec 18 '12 at 08:49
1

You are pushing a "object reference" to the array. When you change tObj after your first tArray.push(tObj);, your changes are also reflected in the object already in the array (Since that is the same object. A "object reference" is pretty much only a pointer to a object. Push a object to a array twice, and you have 2 pointers to the same object.)

Try using the object literal notation:

tArray.push({
    "TranslatedLocIpAddr": 202116107,
    "TranslatedLocPort": 123,
    "LocIp": 50529027,
    "LocPort": 324,
    "LocPortRange": 5
});

This will create a new object every time you push, without the references interfering.
(And it's less code ;-) )

Cerbrus
  • 70,800
  • 18
  • 132
  • 147