JavaScript数组填充

时间:2014-12-15 20:58:52

标签: javascript jquery arrays

我对下一个代码有疑问,我该如何填写'事件'动态数组,带有for / while? (由于有很多数据,我无法手动填写) 谢谢

<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>

2 个答案:

答案 0 :(得分:3)

您可以使用IIFE(立即调用的函数表达式)

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;

})() 

答案 1 :(得分:2)

您可以创建一个数组变量,然后在初始化日历时使用它,如下所示:

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
});