将元素添加到...中的数组内部

时间:2014-07-31 18:05:22

标签: javascript arrays json

我试图用json数据制作一个二维数组。第一个数组在for循环中生成,然后被推送到顶级数组。我试图打印数组,每个元素都得到相同的值,这是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": ""
      }
    }
  }
]

代码:

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"

2 个答案:

答案 0 :(得分:0)

Place" var top = []"在循环内部并将array.push(top)更改为array.unshift(top),unshift方法始终将元素插入索引0。

答案 1 :(得分:0)

这是因为您在循环范围之外初始化top并使top[0]覆盖top数组的所有引用,这些数组被保存在{ {1}}

array声明置于循环中并查看差异

top

http://jsfiddle.net/sUpC8/

如果你坚持在循环范围之外,你可以通过克隆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; } 数组来解决这个问题

top