creating an array in JavaScript on given format

71 Views Asked by At

how can I create an array in the format below:


  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/'
                    }
                ];

I've tried like that:


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

but it doesn't display in correct format, how can I implement this

2

There are 2 best solutions below

3
On

You can create options before loop, and then in loop push objects:

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

You can try 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