Kira
Kira

Reputation: 1207

Fill javascript object dynamically

I'm using responsive calendar in a mvc project. When setting up the calendar, I need to fill an object called events with my data.

$(".responsive-calendar").responsiveCalendar({
            time: '@DateTime.Now.Year.ToString()' + '-' + '@DateTime.Now.Month.ToString()',
            events: { //object to fill with my model data
            "2013-04-30": { "number": 5, "url": "http://w3widgets.com/responsive-slider" },
            "2013-04-26": { "number": 1, "url": "http://w3widgets.com" },
            "2013-05-03": { "number": 1 },
            "2013-06-12": {},
            "2015-06-12": { "number": 1 }
       }
  });

However this object isn't an array. How to achieve this

Update : My model is a list of DateEvents :

class DateEvents 
{ 
    DateTime Date {get;set;} 
    int Count {get;set;}
}

Upvotes: 0

Views: 590

Answers (1)

Björn Boxstart
Björn Boxstart

Reputation: 1168

When you have an array with data, but must supply it like in your example in the 'events' property, you can create an object like below.

var myEvents = {};
myEvents["2013-04-30"] = { "number": 5, "url": "http://w3widgets.com/responsive-slider" };

If you can do this for one item, you can do this also in a loop (forEach) to get the data from an existing source (array?) and add it to the myEvents object. After completion of 'myEvents', you can set the value of 'events' of the responsiveCalendar to 'myEvents'.

Upvotes: 1

Related Questions