Subasri
Subasri

Reputation: 63

Iterate over objects

I know this question is asked many times but i could not find a solution for this and i am new to jquery I am building a calendar to display the shifts for multiple days for an employee. I have hard-coded the value of title,start and end and this codes works fine. I will fetch the values of title,start and end from the back end. I ll be getting multiple values.so I have to loop through the values and pass all the values within the events object.

I will be getting values like

[morningShift,10/09/2018,20/09/2018],[eveningShift,21/09/2018,23/09/2018]

I should pass the values within title start date and end date dynamically and it should form like this

{
            title: 'morningShift',
            start: new Date('10/09/2018'),
            end: new Date('20/09/2018'),
            className: 'bg-primary'
        },
         {
            title: 'eveningShift',
            start: new Date('21/09/2018'),
            end: new Date('23/09/2018'),
            className: 'bg-primary'
        }

 var calendar = $('#calendar').fullCalendar({
        slotDuration: '00:15:00', /* If we want to split day time each 15minutes */
        minTime: '00:00:00', /* calendar start Timing */
        maxTime: '24:00:00',  /* calendar end Timing */
        defaultView: 'month',  
        handleWindowResize: true,   
        height: $(window).height() - 200,   
        header: {
            left: 'prev,next today',
            center: 'title',
            right: 'month,agendaWeek,agendaDay'
            // right: ''

        },
        events: [
           {
            title: 'Morning Shift',
            start: new Date('10/25/2018'),
            end: new Date('11/05/2018'),
            className: 'bg-primary'
        },

    ],
        editable: true,
        droppable: true, // this allows things to be dropped onto the calendar !!!
        eventLimit: true, // allow "more" link when too many events
        selectable: true,

        select: function (start, end, allDay) {
            $modal.modal({ 
                backdrop: 'static'
            });


            calendar.fullCalendar('unselect');
        }
    });

Upvotes: 0

Views: 43

Answers (1)

Mohammed Ashfaq
Mohammed Ashfaq

Reputation: 3426

new Date(mm/dd/yyyy) accept the value in this format.

let apiData = [["morningShift","5/09/2018","10/09/2018"],['eveningShift','1/09/2018','10/09/2018']]
events = apiData.map(([title, start, end])=>({
            title,
            start: new Date(start),
            end: new Date(end),
            className: 'bg-primary'})
          )



console.log(events)

Upvotes: 1

Related Questions