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?

rene
  • 41,474
  • 78
  • 114
  • 152
Antegeia
  • 271
  • 3
  • 18

8 Answers8

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');
Arnold Daniels
  • 16,516
  • 4
  • 53
  • 82
0

Try this :

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

console.log(dayValues); // outputs ["2", "3", "4"]
Jeremy Thille
  • 26,047
  • 12
  • 43
  • 63
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

Chris Charles
  • 4,406
  • 17
  • 31
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]
AngularLover
  • 364
  • 1
  • 12
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

VaMoose
  • 196
  • 7
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);
prog1011
  • 3,425
  • 3
  • 30
  • 57
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));
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
});
enguerranws
  • 8,087
  • 8
  • 49
  • 97