遍历Ajax数据

i have a ajax call that gets me a json containing holiday dates. Now i want to convert the dates to moments and store them in an array. But my code doesn't seem to work properly. I just get empty arrays.

var holidays=[]
$.ajax({
    url: 'holiday.html',
    type: 'POST',
    data: {
        type: 'getHolidays',
        start: intervalStart,
        end: intervalEnd,
    },
    datatype: 'json',
    success: function(data){
        $.each(data,function(index,holiday){
            holidays.push(moment.unix(holiday["date"]));
        });
    },
});

The Json looks like this

[
    {
        "date":"1545696000",
        "title":"Christmas Day1"
    },
    {
        "date":"1545782400",
        "title":"Christmas Day2"
    }
]

Your code is completely fine you just need to handle async behaviour of javascript. Like you have to use holidays array after sucsess

var holidays=[]
$.ajax({
    url: 'holiday.html',
    type: 'POST',
    data: {
        type: 'getHolidays',
        start: intervalStart,
        end: intervalEnd,
    },
    datatype: 'json',
    success: function(data){
        $.each(data,function(index,holiday){
            holidays.push(moment.unix(holiday["date"]));
            call_your_remaining_code_here();
        });
    },
});

Add this to your ajax call;

async : false