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.
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: