在Javascript中动态创建[]数组 [英] create []array dynamically in Javascript
本文介绍了在Javascript中动态创建[]数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这可能是一个真正愚蠢的问题,但我是javascript中的新手,并坚持动态创建数组,如下面的格式:
Probably this will be a real stupid question to ask but im new in javascript and stuck with dynamic creation of array like in below format:
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语法动态创建上面的数组。
Please guide me how do i create above array dynamically using javascript syntax.
提前致谢!
推荐答案
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));
参见:
- Javascript 阵列
-
Array.prototype.push
添加元素
这篇关于在Javascript中动态创建[]数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文