简体   繁体   中英

JavaScript Array Fill

I have a question regarding the next code, how can I fill the 'events' array dynamically, with a for / while ? ( I can't fill it manually due to the fact that there are a lot of datas ) Thank you

<script>

$(document).ready(function() {

    $('#calendar').fullCalendar({
        header: {
            left: 'prev,next today',
            center: 'title',
            right: 'month,basicWeek'
        },
        defaultDate: '2014-12-15',
        editable: true,
        eventLimit: true, // allow "more" link when too many events
        events: [
            {
                title: 'Test',
                start: '2014-12-17'
            }
        ]
    });

});

</script>

You can use IIFE (immediately-invoked function expression)

events: (function () {

    var events = [];

    for (var i = 0; i < 10; i +=1) {

        // You can do here anything.

        events.push({
            title: 'Test' + i,
            start: '2014-12-17'
        });
    }

    return events;

})() 

You can create an array variable and then use it when you initialize the calendar like so:

var events = [];
for(var i = 0; i < 10; i++) {
    events.push({title: 'Test' + i, start: '2014-12-17'});
}

$('#calendar').fullCalendar({
    header: {
        left: 'prev,next today',
        center: 'title',
        right: 'month,basicWeek'
    },
    defaultDate: '2014-12-15',
    editable: true,
    eventLimit: true, // allow "more" link when too many events
    events: events
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM