I saw other questions and I tried the filter function, but I'm not sure if I use 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 go through many days, and every day I go through the hours between 6 AM and 9 PM. If there is an hour in the above array (like an hour on that particular date). I want to mark it as available in a new object, which 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 few notes. I use .toString () in the day.date property because it is formatted in the JS date format, and the value I compare with it is in the MYSQL date format. I warned (talking about old school debugging) thisIsAvailable.length, and I get 0 every time. Any ideas are welcome. Thanks!
EDIT: Just realized that I forgot to tell you everything very important. The array that I give you is contained in the variable Named assignHours. Sorry it didn't work out.
EDIT 2: To clarify, my question is a bit between two pieces of code. I am trying to see if one of the objects in this array corresponds to the day and hour in the loop, and the nested loop through which I execute. If so, I want to pass this to a new object, which I then insert into the array of hours of the day. If not, I pass the object anyway, but with a 0 value for its availability.