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.
CodePudding user response:
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;
})
CodePudding user response:
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; }