4

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.

4

2 回答 2

3

我怀疑您的问题在于您使用 jQuery 方法的filter方式:

var thisIsAvailable = $(assignedHours).filter(function(){
    return assignedHours.hour == thisHour && assignedHours.date == day.date.toString("yyyy-MM-dd");
});

如果我没记错的话,assignedHours.hour将永远是未定义的,因为它总是引用你的assignedHours数组(你希望它引用你正在过滤的当前元素,即里面 assignedHours的元素)。

你可能想要做这样的事情:

var thisIsAvailable = $(assignedHours).filter(function(array_key){
    return this.hour == thisHour && this.date == day.date.toString("yyyy-MM-dd");
});

Edit1:正如@Kevin B 所指出的,$.grep它更适合过滤数组,因为它不会在您的数组周围创建 DOM 包装器:

$.grep(assignedHours, function(el) {
    return el.hour == thisHour && el.date == day.date.toString("yyyy-MM-dd");
});
于 2012-11-01T14:28:06.487 回答
2

应 OP 的要求从评论提升为答案:

您应该检查this.hourthis.date在您的过滤器功能中。

于 2012-11-01T14:30:12.197 回答