在Javascript中动态创建[]数组

时间:2014-02-04 13:36:18

标签: javascript

这可能是一个真正愚蠢的问题,但我是javascript的新手,并坚持动态创建数组,如下面的格式:

items = [{
    "Date": "2012-01-21T23:45:10.280Z",
    "Value": 7
}, {
    "Date": "2012-01-26T23:45:10.280Z",
    "Value": 10
}, {
    "Date": "2012-05-30T23:45:10.280Z",
    "Value": 16
}];

请指导我如何使用javascript语法动态创建上面的数组。

提前致谢!

2 个答案:

答案 0 :(得分:3)

var items = []; // initialize array
items.push({ // add 1st item
  "Date": "2012-01-21T23:45:10.280Z",
  "Value": 7
});
items.push({ // add 2nd item
  "Date": "2012-01-26T23:45:10.280Z",
  "Value": 10
});
items.push({ // add 3rd item
  "Date": "2012-05-30T23:45:10.280Z",
  "Value": 16
});

并查看它:

console.log(JSON.stringify(items));

请参阅:

答案 1 :(得分:1)

你的意思是这样的:

items = [];

items.push({
    "Date": "2012-01-21T23:45:10.280Z",
    "Value": 7
});
items.push({
    "Date": "2012-01-26T23:45:10.280Z",
    "Value": 10
});
items.push({
    "Date": "2012-05-30T23:45:10.280Z",
    "Value": 16
});