-2

我想创建一个看起来像这样的变量:

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"
  }
];

如何动态创建这样的变量?我会在一个循环中写这个,我想继续向声明的变量添加值。

4

2 回答 2

3

尝试

var data = [];
var objPush = {};
objPush.value = "jquery";
objPush.label = "jQuery";
objPush.desc = "the write less, do more, JavaScript library";

data.push(objPush);
于 2013-04-23T04:51:19.657 回答
1

为什么不

var projects = [];
projects.push({
    value : "<some-value>",
    label : "<some label>",
    desc : "<description>"
});
projects.push({
    value : "<some-value>",
    label : "<some label>",
    desc : "<description>"
});
....
于 2013-04-23T04:51:17.503 回答