Reputation: 344
I have a timepicker that works just fine except when I am trying to disableTimeRanges. I have some code that assembles this string:
var disableTimeRanges = '[';
var count = jsonObject.length;
var i = 0;
$.each(jsonObject, function (i, obj) {
disableTimeRanges += "['" + obj.appointmentStart.split(" ").slice(1, 3).join('') + "','" + obj.appointmentEnd.split(" ").slice(1, 3).join('') + "']";
i++;
if (i < count)
disableTimeRanges += ',';
});// end $.each
disableTimeRanges += ']';
at this point, disableTimeRanges = [['11:30:00AM','12:00:00PM'],['12:30:00PM','1:30:00PM']]
Then,
$('#appointmentTimeTextbox').timepicker('option', {
'disableTimeRanges': disableTimeRanges
});
I know I am doing something wrong because the chrome debugger is giving me:
Uncaught TypeError: b.disableTimeRanges.sort is not a function
Is there a problem with the array? Is there a better way to assemble the array?
Edit: fixed as an array:
var disableTimeRanges = new Array();
var appointmentTime = [obj.appointmentStart.split(" ").slice(1, 3).join(''),
obj.appointmentEnd.split(" ").slice(1, 3).join('')];
disableTimeRanges.push(appointmentTime);
Upvotes: 0
Views: 1164
Reputation: 2095
disableTimeRanges
is not an array in your code snippet, it's a string.
Something like this should do:
var disableTimeRanges = [];
var count = jsonObject.length;
$.each(jsonObject, function (i, obj) {
inRange = []
inRange.push(obj.appointmentStart.split(" ").slice(1, 3).join(''));
inRange.push(obj.appointmentEnd.split(" ").slice(1, 3).join(''));
disableTimeRanges.push(inRange);
});
Upvotes: 1
Reputation: 15951
Is there a problem with the array?
Yes, its not an array. It's a string. While an array prints out as ['A', 'B', 'C']
it is not the same as the string "['A','B','C']"
If you want to construct an array, construct an array.
var cars = ["Saab", "Volvo", "BMW"];
https://www.w3schools.com/js/js_arrays.asp
Upvotes: 1