1

I am trying to create a booking app and mark out the slots unavailable. The slots unavailable are in an array for example:

const unavailable = [ "11:00am a 12:00pm", "17:00pm a 18:00pm"]

and the total slots are

 let slots = [ {
    slot: "9:00am a 10:00am",
    isReserved: false,
  },
  {
    slot: "10:00am a 11:00am",
    isReserved: false,
  },
  { slot: "11:00am a 12:00pm", isReserved: false },
  {
    slot: "15:00pm a 16:00pm",
    isReserved: false,
  },
  {
    slot: "17:00pm a 18:00pm",
    isReserved: false,
  },
  {
    slot: "18:00pm a 19:00pm",
    isReserved: false,
  },
  { slot: "19:00pm a 20:00pm", isReserved: false },
];

my goal is to use the unavailable array and each value inside and find how to change that field to

isReserved: true

I've tried to filter like so

    const matches = slots.filter((same) => same.slot === unavailable);

but does not work. What did kind of work was

    const matches = slots.filter((same) => same.slot === unavailable[0]);

but that only matches the first value, and I still do not know how to change the isReserved field.

2
  • 1
    do you want a new array or mutate the old one? Commented Dec 13, 2021 at 18:11
  • i would like to mutate, but either or should work for what I need Commented Dec 13, 2021 at 22:07

2 Answers 2

1

You could iterate and update if a slot is found.

const
    unavailable = ["11:00am a 12:00pm", "17:00pm a 18:00pm"],
    slots = [{ slot: "9:00am a 10:00am", isReserved: false }, { slot: "10:00am a 11:00am", isReserved: false }, { slot: "11:00am a 12:00pm", isReserved: false }, { slot: "15:00pm a 16:00pm", isReserved: false }, { slot: "17:00pm a 18:00pm", isReserved: false }, { slot: "18:00pm a 19:00pm", isReserved: false }, { slot: "19:00pm a 20:00pm", isReserved: false }];

slots.forEach(o => {
    if (unavailable.some(slot => slot === o.slot)) o.isReserved = true;
});

console.log(slots);
.as-console-wrapper { max-height: 100% !important; top: 0; }

If you like not to mutate the original array, you could map the objects with a new property.

const
    unavailable = ["11:00am a 12:00pm", "17:00pm a 18:00pm"],
    slots = [{ slot: "9:00am a 10:00am", isReserved: false }, { slot: "10:00am a 11:00am", isReserved: false }, { slot: "11:00am a 12:00pm", isReserved: false }, { slot: "15:00pm a 16:00pm", isReserved: false }, { slot: "17:00pm a 18:00pm", isReserved: false }, { slot: "18:00pm a 19:00pm", isReserved: false }, { slot: "19:00pm a 20:00pm", isReserved: false }],
    result = slots.map(o => ({
        ...o,
        isReserved: o.isReserved || unavailable.some(slot => slot === o.slot)
    }));

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }

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

1 Comment

the second option you have provided worked perfectly! saved me so much time thank you very much!
1

you can use the map operator and can achieve this.

const unavailable = [ "11:00am a 12:00pm", "17:00pm a 18:00pm"]
let slots = [ {
    slot: "9:00am a 10:00am",
    isReserved: false,
  },
  {
    slot: "10:00am a 11:00am",
    isReserved: false,
  },
  { slot: "11:00am a 12:00pm", isReserved: false },
  {
    slot: "15:00pm a 16:00pm",
    isReserved: false,
  },
  {
    slot: "17:00pm a 18:00pm",
    isReserved: false,
  },
  {
    slot: "18:00pm a 19:00pm",
    isReserved: false,
  },
  { slot: "19:00pm a 20:00pm", isReserved: false },
];

const slots = slots.map(slotobj=>{
if(unavailable.includes(slotobj.slot)){
 slotobj.isReserved =true
}
return slotobj;
})

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.