I'm trying to populate an array of objects in Javascript. If I check the array item using the index immediately after pushing it onto the array it looks good. However, if I try to use the elements of the array after it is populated, the whole array seems to be populated with copies of the last object I pushed. I have the same problem in both Chrome and Firefox. I have attached a simplified version of my code that demonstrates the issue. Any guidance would be appreciated.
<script type="text/javascript">
var JSONHLDat=[
[
"2",
"1",
"72.2",
"52.1"
],
[
"2",
"2",
"72.2",
"52.2"
],
[
"2",
"3",
"72.3",
"52.4"
],
[
"2",
"4",
"72.4",
"52.5"
]
];//data to be added to array
var oHiLo ={
oMonth: 0,
oDay: 0,
oHi: 0,
oLo: 0
};//oHiLo is the object
var HiLo=[];//HiLo is the array to contain multiple oHiLo objects
for (var j in JSONHLDat)
{
oHiLo.oMonth = JSONHLDat[j][0];
oHiLo.oDay = JSONHLDat[j][1];
oHiLo.oHi = JSONHLDat[j][2];
oHiLo.oLo = JSONHLDat[j][3];
console.log("oHiLo=", oHiLo);// here I display the object, it looks good.
var ndx = HiLo.push(oHiLo);
console.log("ndx=", ndx, " HiLo[(ndx-1)] = ", HiLo[(ndx - 1)]);//here I display the object in HiLo. It looks good.
}
for (var k=0;k<4;k++)
{
console.log("k=", k, " HiLo[k] = ", HiLo[k]);// This appears to show that the array is populated with four copies of the last element pushed.
}
</script>