0

I'm working on a project that filters jobs depending on job schedules and personal availability of users.

const jobs: JobRecord[] = [
  {
    "storeName": "Highwire Coffee Company",
    "schedule":  [
      "Tuesday",
      "Thursday",
    ],
        ...
  },
  {
    "storeName": "Blue Bottle Coffee",
    "rid": "recNgeUMcYhWUxw8b",
    "schedule":  [
      "Thursday",
    ],
    ...
  },
] 

const availability: Availability = {
  "friday": false,
  "monday": false,
  "thursday": true,
  "tuesday": false,
  "wednesday": true,
}

For example, given these inputs above, I should make a function returning only the job with storename "Blue Bottle Coffee".

To that end, I've written these two functions.

  findAvailableDays (availability: Availability){
    var availDays: string[];
    availDays = [];
    for (const [day, avail] of Object.entries(availability)){
      if (avail){
        availDays.push(day);
      }
    }
    return availDays;
  };

  filterJobs = (jobs: JobRecord[], availability: Availability): void => {
    // Step 0: Clone the jobs input
    const newJobs: JobRecord[] = cloneDeep(jobs);
    console.log(newJobs, availability);

    // Step 1: Remove jobs where the schedule doesn't align with the users' availability.
    for (const[store, schedule] of Object.entries(jobs)){
      var availDays: string[];
      availDays = this.findAvailableDays(availability);
      const checkIncludes = (currentValue) => availDays.includes(currentValue);
      let checker = (availDays, schedule) => schedule.every(checkIncludes(checkIncludes));
      if(!checker(availDays, schedule)){
        delete newJobs[store];
      }
    }
    // Step 2: Save into state
    this.setState({ jobs: newJobs });
  };

However, my every() function in filterJobs always runs into an error (shown below). I've checked that every() accepts functions as arguments (which I made sure), and works on arrays (I believe schedule should be an array, given the example). What am I doing wrong/missing here? Should I try to find another way to go about this?

enter image description here

1
  • Please post your JobRecord type code. Commented Sep 12, 2020 at 3:06

1 Answer 1

1

schedule isn’t an array, it’s a JobRecord. You probably meant this, where schedule is destructured from the JobRecord:

for (const [store, {schedule}] of Object.entries(jobs)) {

However, the better way to write the whole thing is without deleting by index – use filter instead:

filterJobs = (jobs: JobRecord[], availability: Availability): void => {
  const availDays = this.findAvailableDays(availability);

  const filteredJobs = jobs.filter(({schedule}) =>
    schedule.every(day => availDays.includes(day)));

  this.setState({ jobs: filteredJobs });
};

It looks like you might need to consider the case difference between the property names of availability and the elements of schedule, by the way.

Sign up to request clarification or add additional context in comments.

6 Comments

that does seem to get rid of the errors I encountered previously, but fails to return "Blue Bottle Coffee" in the example case. I'm trying to figure out why—do you know why this might be the case?
@singularity: “It looks like you might need to consider the case difference between the property names of availability and the elements of schedule, by the way.” Confirm with a debugger if you have one! console.log otherwise.
ah, I finally see what you mean! I just tried schedule.every(day.toLowerCase() => availDays.includes(day.toLowerCase()))), but they don't seem to accept the change (error). Does that mean I should modify my inputs into availDays?
@singularity: The left of => is the parameter list for the function. It doesn’t make sense to put an expression like day.toLowerCase() there. Just schedule.every(day => availDays.includes(day.toLowerCase()))).
thank you! I do have one more question, though. Is there any way you can modify newJobs rather than create an entirely new array? The way I was given the shell code, it seems newJobs should be modifiable, but newJobs is apparently "read-only".
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.