I've seen other questions, and I've tried the filter function, but I'm not sure if I'm using it correctly.
Essentially I have an array of objects that looks like this:
[{"hour":"6 am", "date":"2012-12-01"},{"hour":"7 am", "date":"2012-12-01"}]
I'm looping through a set period of days, and on each day I'm looping through the hours between 6am and 9pm. If the hour(as in the hour on that specific date) exists in the above array. I want to mark it as available in a new object that I then pass to a new array. Below is the code I'm currently using.
for(var i = 0; i < dayCount; i++){//Loop through the days that exist in the schedule
day = new Object();
day.date = Date.parse(startDate).add(i).days();
day.dayName = weekday[day.date.getDay()]
day.hours = new Array();
for(var j = 6; j < 22; j++){ //Loop through hours of the day seeing if they're available/scheduled, etc.
if(j<=12){
thisHour = j +' am';
} else{
thisHour = j-12 + ' pm';
}
var thisIsAvailable = $(assignedHours).filter(function(){
return assignedHours.hour == thisHour && assignedHours.date == day.date.toString("yyyy-MM-dd");
});
var thisIsScheduled = 0;
day.hours.push({hour: thisHour,available: thisIsAvailable, scheduled: thisIsScheduled});
}
daysInSchedule.push(day);
}
A couple notes. I use .toString() on the day.date property because it is formatted in JS Date format and the value I'm comparing it against is in the MYSQL Date format. I've alerted (talk about old school debugging) thisIsAvailable.length, and I get 0 everytime. Any ideas are appreciated. Thanks!
EDIT: Just realized I forgot to tell you all something very important. The array I give you is contained in the variable Named assignedHours. Sorry about leaving that out.
EDIT 2: To clarify, my question is in the bit between the two code excerpts. I'm trying to see if one of the objects in the given array matches the day and hour in the loop, and nested loop I'm running through. If it does I want to pass that on to a new object which I then push into the hours array of the day. If not then I still pass the object but with a 0 value for it being available.