在javascript / jQuery中动态创建数组对象

时间:2013-04-23 04:47:04

标签: javascript jquery arrays multidimensional-array

我想创建一个如下所示的变量:

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

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

2 个答案:

答案 0 :(得分:2)

尝试

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

data.push(objPush);

答案 1 :(得分:1)

为什么不

var projects = [];
projects.push({
    value : "<some-value>",
    label : "<some label>",
    desc : "<description>"
});
projects.push({
    value : "<some-value>",
    label : "<some label>",
    desc : "<description>"
});
....