输出javascript数组的内容

时间:2015-06-15 15:37:58

标签: javascript jquery arrays

我需要将下面“events”中的日期替换为JSON调用返回的日期列表,这些日期存储在“scheduledDates”中。有人可以帮忙吗?

我也需要在每个日期保留:{}。

<script type="text/javascript">
var today = new Date();
var dd = today.getDate();
var mm = today.getMonth() + 1;
var yyyy = today.getFullYear();

var currentDate = yyyy + '-' + mm;

var bookedDates = [];

$(document).ready(function () {
    $.ajax({
        type: "POST",
        url: "/Ajax/getBooked",
        data: "{}",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (response) {
            bookedDates = bookedDates.concat(response),
            bookedDates.toString;
        }
    });
    $(".responsive-calendar").responsiveCalendar({
        time: currentDate,
        events: {
            "2015-06-30": {}
        }
    });
});
</script>

2 个答案:

答案 0 :(得分:2)

试试这个,

events = {};

for (i = 0; i < bookedDates.length; i++) {
     events[bookedDates[i]] = {};
}

Demo plunk:http://plnkr.co/edit/jz6cqrFN92DOrAekFfC8?p=preview, 转到控制台查看事件对象

答案 1 :(得分:0)

您需要遍历返回的数组并创建一个对象,每个键都是response.responseJSON对象中的日期。

需要在.responsiveCalendar({回调中调用success函数,以便您可以使用从AJAX调用返回的数据。

var today = new Date();
var dd = today.getDate();
var mm = today.getMonth() + 1;
var yyyy = today.getFullYear();

var currentDate = yyyy + '-' + mm;

//commented unless you are using this data in another part of the program
//var bookedDates = [];

$(document).ready(function () {
    $.ajax({
        type: "POST",
        url: "/Ajax/getBooked",
        data: "{}",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (response) {
            //commented unless you are using this data in another part of the program
            //bookedDates = bookedDates.concat(response),
            //bookedDates.toString;

            //Assuming response.responseJSON = ["2015-06-15","2015-06-16","2015-06-17","2015-06-20","2015-06-22","2015-06-23"]
            var events = {};
            response.responseJSON.forEach(function (date) {
                events[date] = {}
            });

            $(".responsive-calendar").responsiveCalendar({
                time: currentDate,
                events: events
            });
        }
    });
});