0

I'm trying to make a 2 dimensional array with a json data. The first array is made within a for in loop and is pushed to the top-level array after. I tried to print the array and I got same value for each elements which is the last data from the json.

json:

[
  {
    "startYear": 2014,
    "startMonth": 6,
    "startDay": 31,
    "endYear": 2014,
    "endMonth": 7,
    "endDay": 29,
    "selectedDate": "2014_7_8",
    "departureStation": "Manila",
    "arrivalStation": "Boracay (Caticlan)",
    "departureStationCode": "(MNL)",
    "arrivalStationCode": "(MPH)",
    "departureLabel": "DEPARTURE",
    "arrivalLabel": "RETURN",
    "dateMarketHash": {
      "date_0_2014_6_31": {
        "containerId": "date_0_2014_6_31",
        "fromLabel": "From",
        "currency": "PHP",
        "price": null,
        "formattedDate": "Thu, Jul 31, 2014", //data to get
        "year": "2014",
        "month": "6",
        "day": "31",
        "points": null,
        "pointsSuffix": "",
        "pointsLabelAppend": ""
      },
      "date_0_2014_7_1": {
        "containerId": "date_0_2014_7_1",
        "fromLabel": "From",
        "currency": "PHP",
        "price": 1929,
        "formattedDate": "Fri, Aug 01, 2014", //data to get
        "year": "2014",
        "month": "7",
        "day": "1",
        "points": 0,
        "pointsSuffix": "",
        "pointsLabelAppend": ""
      }
    }
  },
  {
    "startYear": 2014,
    "startMonth": 7,
    "startDay": 24,
    "endYear": 2014,
    "endMonth": 8,
    "endDay": 23,
    "selectedDate": "2014_8_8",
    "departureStation": "Boracay (Caticlan)",
    "arrivalStation": "Manila",
    "departureStationCode": "(MPH)",
    "arrivalStationCode": "(MNL)",
    "departureLabel": "DEPARTURE",
    "arrivalLabel": "RETURN",
    "dateMarketHash": {
      "date_1_2014_7_24": {
        "containerId": "date_1_2014_7_24",
        "fromLabel": "From",
        "currency": "PHP",
        "price": 3079,
        "formattedDate": "Sun, Aug 24, 2014",
        "year": "2014",
        "month": "7",
        "day": "24",
        "points": 0,
        "pointsSuffix": "",
        "pointsLabelAppend": ""
      },
      "date_1_2014_7_25": {
        "containerId": "date_1_2014_7_25",
        "fromLabel": "From",
        "currency": "PHP",
        "price": 3079,
        "formattedDate": "Mon, Aug 25, 2014",
        "year": "2014",
        "month": "7",
        "day": "25",
        "points": 0,
        "pointsSuffix": "",
        "pointsLabelAppend": ""
      }
    }
  }
]

code:

var current = json[0].dateMarketHash;
var top = [];
var array = [];
for(var key in current){
    top[0] = current[key].formattedDate;
    top[1] = current[key].currency;
    top[2] = current[key].price;
    array.push(top);
}       

document.write(array[0][0]); //prints "Fri, Aug 01, 2014" instead of "Thu, Jul 31, 2014"
document.write(array[1][0]); //prints "Fri, Aug 01, 2014"
dgzz
  • 2,929
  • 6
  • 34
  • 56

2 Answers2

0

Place "var top=[]" inside the loop and change array.push(top) to array.unshift(top), unshift method always insert element to the index 0.

Allen
  • 6,745
  • 5
  • 41
  • 59
0

It is because you initialize top outside the loop scope and that makes top[0] overwrite all references to the top array, which are being held in array

Put the top declaration inside the loop and see the difference

var current = json[0].dateMarketHash;
var array = [];
for(var key in current){
    var top = [];
    top[0] = current[key].formattedDate;
    top[1] = current[key].currency;
    top[2] = current[key].price;
    array[array.length] = top;
}  

http://jsfiddle.net/sUpC8/

If you insist on top being outside the loop scope you can work-around this problem by cloning the top array

var current = json[0].dateMarketHash;
var array = [];
var top = [];
for(var key in current){
    top[0] = current[key].formattedDate;
    top[1] = current[key].currency;
    top[2] = current[key].price;
    array[array.length] = top.slice(0);
}
php_nub_qq
  • 15,199
  • 21
  • 74
  • 144