0

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.

2
  • Can you give us a sample of the JSON response? Commented Jun 15, 2015 at 15:10
  • Here you go: ["2015-06-15","2015-06-16","2015-06-17","2015-06-20","2015-06-22","2015-06-23"] Commented Jun 15, 2015 at 15:12

1 Answer 1

3

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);
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks you. I've to use the concat method. How do i use an alert to display the contents of my array, to test that it has worked? Many thanks
Don't worry, done it. Works a treat. Thanks so much.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.