我想创建一个像这样的变量:
var projects = [
{
value: "jquery",
label: "jQuery",
desc: "the write less, do more, JavaScript library"
},
{
value: "jquery-ui",
label: "jQuery UI",
desc: "the official user interface library for jQuery"
},
{
value: "sizzlejs",
label: "Sizzle JS",
desc: "a pure-JavaScript CSS selector engine"
}
];
如何动态创建这样的变量?我会在一个循环中写这个,我想继续向声明的变量添加值。
Try
var data = [];
var objPush = {};
objPush.value = "jquery";
objPush.label = "jQuery";
objPush.desc = "the write less, do more, JavaScript library";
data.push(objPush);
为什么不
var projects = [];
projects.push({
value : "<some-value>",
label : "<some label>",
desc : "<description>"
});
projects.push({
value : "<some-value>",
label : "<some label>",
desc : "<description>"
});
....