Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8773291
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T18:15:27+00:00 2026-06-13T18:15:27+00:00

I’ve seen other questions, and I’ve tried the filter function, but I’m not sure

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T18:15:28+00:00Added an answer on June 13, 2026 at 6:15 pm

    I suspect that your problem lies in the way you are using jQuery’s filter method:

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

    If I’m not mistaken, assignedHours.hour will always be undefined because it always references your assignedHours array (where you’d want it to reference the current element you are filtering, i.e. an element inside assignedHours).

    You’ll probably want to do something like this:

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

    Edit1: As pointed out by @Kevin B, $.grep is better suited for filtering arrays as it does not create a DOM wrapper around your array:

    $.grep(assignedHours, function(el) {
        return el.hour == thisHour && el.date == day.date.toString("yyyy-MM-dd");
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have just tried to save a simple *.rtf file with some websites and
I have a French site that I want to parse, but am running into
This could be a duplicate question, but I have no idea what search terms
I have an array which has BIG numbers and small numbers in it. I
I know there's a lot of other questions out there that deal with this
I need a function that will clean a strings' special characters. I do NOT
I want to construct a data frame in an Rcpp function, but when I
I have thousands of HTML files to process using Groovy/Java and I need to
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.