Gavin5511
Gavin5511

Reputation: 791

Adding JSON results to JavaScript list

I have created a script to call a page, which returns a list of date strings via JSON. How do I add these dates to an array that I've created?

var bookedDates= [];
$(document).ready(function () {
    $.ajax({
        url: "/Ajax/getBooked",
        data: "{}",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (response) {
            alert("success");
            //Add results to list here?
        }
    });
});

I'm new to JavaScript.

Upvotes: 0

Views: 42

Answers (1)

Satpal
Satpal

Reputation: 133453

You can use Array.prototype.concat()

The concat() method returns a new array comprised of the array on which it is called joined with the array(s) and/or value(s) provided as arguments.

var new_array = old_array.concat(value1[, value2[, ...[, valueN]]])

Code

bookedDates = bookedDates.concat(response)

Or, Use Array.prototype.push()

The push() method adds one or more elements to the end of an array and returns the new length of the array

Code

Array.prototype.push.apply(bookedDates, response);

Upvotes: 3

Related Questions