1

I try to filter the array based on const array inside the for-loop, my problem is that my filtered array is not being saved.

The main array that I use:

const allHours = [
  "10:00",
  "11:00",
  "12:00",
  "13:00",
  "14:00",
  "15:00",
  "16:00",
  "17:00",
  "18:00",
  "19:00",
];
let availableHours=[];

I have an empty array when I catch an hour that is occupied. I want to filter out the hour and get back a new array with the same allHours without the filtering being applied.

for (let i = 0; i < 7; i++) {
  const date = moment().utc().add(i, "days");

  if (date.weekday() === 5 || date.weekday() === 6) {
    continue;
  } else {
    if (meetings.map(d => {
      if (moment(d.date).utc().startOf("day").isSame(date.startOf("day"), "days")) {
        availableHours = allHours.filter(h => h == moment(d.date).utc().format("LT"))

For example, I have in this day 2 meetings planned at 16:00 and at 18:00.

On the first iteration, it has filtered the 16:00 like expected.

const availableHours = [
  "10:00",
  "11:00",
  "12:00",
  "13:00",
  "14:00",
  "15:00",
  -
  "17:00",
  "18:00",
  "19:00",
];

When it iterates a second time, its start from the beginning, and the available array returns the allHours array without 18:00, The final availableHours array I get is:

const allHours = [
  "10:00",
  "11:00",
  "12:00",
  "13:00",
  "14:00",
  "15:00",
  "16:00",
  "17:00",
  --
  "19:00",
];

Which is wrong. The result should look like this:

const allHours = [
  "10:00",
  "11:00",
  "12:00",
  "13:00",
  "14:00",
  "15:00",
  --
  "17:00",
  --
  "19:00",
];

How can I fix this?

2 Answers 2

1

It easy to fix

First of all, initialize availableHours via allHours;

let availableHours=[...allHours];

then use an availableHours for filtering in meeting loop

availableHours = availableHours.filter(...)
Sign up to request clarification or add additional context in comments.

1 Comment

thanks a lot. its what i looking for. i hope that is right way to do this and not take to much performance
0

you don't need all those loops, one good performant way to do what you need is use Set datatype and clear intersection;

example:

    const allHours = [
    "10:00",
    "11:00",
    "12:00",
    "13:00",
    "14:00",
    "15:00",
    "16:00",
    "17:00",
    "18:00",
    "19:00",
   ];
   const busyHours = new Set(["16:00","18:00"])
   let availableHours= allHours.filter(item=>busyHours.has(item)===false);
   console.log( availableHours );

or you can even use a lookup map;

example:

        const allHours = [
        "10:00",
        "11:00",
        "12:00",
        "13:00",
        "14:00",
        "15:00",
        "16:00",
        "17:00",
        "18:00",
        "19:00",
       ];
       const busyHours = ["16:00", "18:00"]
       // convert array to Object using reduce;
       // { "16:00": true, "18:00": true }
       const busyHoursMap = busyHours.reduce((carry,item)=>({[item]:true, ...carry}),{});
       let availableHours= allHours.filter(item=>busyHoursMap[item]!==true);
       console.log( availableHours );

finally, if your arrays are not too big, then you should not care much about performance, and a simple array filter will do:-

        const allHours = [
        "10:00",
        "11:00",
        "12:00",
        "13:00",
        "14:00",
        "15:00",
        "16:00",
        "17:00",
        "18:00",
        "19:00",
       ];
       const busyHours = ["16:00", "18:00"]
       let availableHours= allHours.filter(item=>busyHours.indexOf(item)>-1);
       console.log( availableHours );

Comments

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.