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?