在给定格式的JavaScript中创建数组

时间:2014-06-02 11:53:32

标签: javascript php zend-framework gcal

如何以下面的格式创建数组:


  var options =
                 [
                    {
                        title: 'Laziness',
                        start: new Date(y, m, d, 12, 0),
                        allDay: false
                    },
                    {
                        title: 'Dizziness,Sleepiness',
                        start: new Date(y, m, d+1, 19, 0),
                        allDay: false
                    },
                    {
                        title: 'Click for Google',
                        start: new Date(y, m, 28),
                        url: 'http://google.com/'
                    }
                ];

我试过这样:


for(var i in data)
           {
             options = {
                    title:data[i].eventNames,
                   start:data[i].eventDates
               };
            }

但它没有以正确的格式显示,我该如何实现

2 个答案:

答案 0 :(得分:0)

你可以试试Like This

  

的Javascript

var arr = [];
var len = 5; //Replace Ur data With Len, Take Data Length or count for loop
for (var i = 0; i < len; i++) {    
    arr.push({
        key: 1 + i,
        value : 1 + i + "Hi"
    });
}

Demo

答案 1 :(得分:-1)

您可以在循环之前创建选项,然后在循环推送对象中创建选项:

options = [];
for(var i in data)
           {
             options.push( {
                    title:data[i].eventNames,
                   start:data[i].eventDates
               })
            }