Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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"
share|improve this question
    
You forgot: "the problem I'm having is..." –  beautifulcoder 18 hours ago
    
@beautifulcoder the problem is right there in the comments of the last code block –  php_nub_qq 18 hours ago
    
You change the value of top everytime, you should create a new top everytime –  Marco Acierno 18 hours ago
    
even if i place "var top=[]" inside the loop, it prints the same –  Daniel John Gomez 18 hours ago
1  
possible duplicate of JavaScript closure inside loops – simple practical example -- place both top and key inside their own closure –  blgt 18 hours ago

2 Answers 2

up vote 0 down vote accepted

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);
}
share|improve this answer
    
still prints same values.. –  Daniel John Gomez 18 hours ago
    
@DanielJohnGomez it prints correct values, did you even see the fiddle? –  php_nub_qq 17 hours ago
    
oops sorry, maybe I just copied the wrong code.. thanks! –  Daniel John Gomez 17 hours ago

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

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.