1

I have a JSON file like this

[{"excursionDay":"2"},{"excursionDay":"3"},{"excursionDay":"4"}]

I want to pass the value of excursionDay into an array in javascript like this

  dayValues = [2,3,4]

My javascript code so far is the following

fucntion excursionDates(date){
var day = date.getDay();
   var dayValues = [];
   $.get('json.php',function(data){
       for(var i=0; i<data.length;i++){
           dayValues.push(data[i].excursionDay);
       }
   },'json');
 }

But for some reason i can't get this work. All of the above i want to triggered when i click in a jquery-ui datepicker and enable only the specific days. The code for the datepicker is bollow.

$("#excursionDate").datepicker({
        minDate: today,
        dateFormat: 'dd-mm-yy',
        beforeShowDay: excursionDates
});

Can anyone help me out please?

6
  • 1
    Possible duplicate of stackoverflow.com/questions/14220321/… Commented Feb 18, 2015 at 8:59
  • You including jquery? Commented Feb 18, 2015 at 9:00
  • Take a look: jQuery.map ... api.jquery.com/jquery.map Commented Feb 18, 2015 at 9:02
  • It working fine check jsfiddle.net/L4s645v2 Commented Feb 18, 2015 at 9:04
  • My console result is [2,3,4] but 28 times cause i am trying to return those values into a jquery-ui calendar to enable only specific days. Commented Feb 18, 2015 at 9:08

8 Answers 8

1

The posted code looks in order, but I'm suspecting that you're not responding with application/json, but with text/html.

Make sure you're doing the following in json.php.

header('Content-Type: application/json');
Sign up to request clarification or add additional context in comments.

Comments

0

Try this :

data.forEach( function(element){
   dayValues.push(element.excursionDay);
})

console.log(dayValues); // outputs ["2", "3", "4"]

Comments

0

This looks like a perfect use case for Array.map:

var startArray = [{"excursionDay":"2"},{"excursionDay":"3"},{"excursionDay":"4"}];

var dayValues = startArray.map(function(item){
                    return item.excursionDay
                });

//dayValues is now ["2","3","4"]

More info on Array.prototype.map

Comments

0

Hay i advise you to use underscore.js.

the method

_.values(object) 

it returns all the values of the object in an array.

_.values({one: 1, two: 2, three: 3});
result: [1, 2, 3]

Comments

0

This can be done easily in JavaScript:

var jsArray = [];
var p = [{"excursionDay":"2"},{"excursionDay":"3"},{"excursionDay":"4"}];
for( var i = 0; i < p.length; i++) {
    jsArray.push(p[i].excursionDay);
}

console.log(jsArray);

jsFiddle

Comments

0

Try this - Use .push() for insert record into array

var dataArray=[{"excursionDay":"2"},{"excursionDay":"3"},{"excursionDay":"4"}];
dayValuesArray = [];
 for(var i=0; i<dataArray.length;i++){
        dayValuesArray.push(dataArray[i].excursionDay);
    }
alert(dayValuesArray);

Comments

0

try below code.

var jsonString ='[{"excursionDay":"2"},{"excursionDay":"3"},"excursionDay":"4"}]';

var obj = $.parseJSON(jsonString);    
var dayValues = [];
for (var i = 0, len = obj.length; i < len; i++){
    dayValues.push(obj[i].excursionDay);
}
console.log(JSON.stringify(obj));

Comments

0

The array part of your code should work fine. But the datepicker call is not.

I guess you should use .beforeShow() method (http://api.jqueryui.com/datepicker/#option-beforeShow), it would work better than .beforeShowDay() that will iterate before each day.

$("#excursionDate").datepicker({
        minDate: today,
        dateFormat: 'dd-mm-yy',
        beforeShow: excursionDates
});

Comments

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.