В цикле проверить совпадение счетчика с массивом
Мне нужно в цикле, указать выходные дни по конкретным месяцам, например месяц 11, если i === 3, то weekendsDisabled = true, и так далее по JSON массиву. В общем, в массиве currentMonthDays у значения weekendsDisabled будет true || false в зависимости от дней из массива weekendsDays Я что-то туплю, как это реализовать, подскажите пожалуйста
weekendsJson: [
{
"month": 1,
"days": "1,2,3,4,5,6,7,8,9,15,16,22,23,29,30"
},
{
"month": 2,
"days": "5,6,12,13,19,20,22*,23,26,27"
},
{
"month": 3,
"days": "5*,6,7+,8,12,13,19,20,26,27"
},
{
"month": 4,
"days": "2,3,9,10,16,17,23,24,30"
},
{
"month": 5,
"days": "1,2+,3+,7,8,9,10+,14,15,21,22,28,29"
},
{
"month": 6,
"days": "4,5,11,12,13+,18,19,25,26"
},
{
"month": 7,
"days": "2,3,9,10,16,17,23,24,30,31"
},
{
"month": 8,
"days": "6,7,13,14,20,21,27,28"
},
{
"month": 9,
"days": "3,4,10,11,17,18,24,25"
},
{
"month": 10,
"days": "1,2,8,9,15,16,22,23,29,30"
},
{
"month": 11,
"days": "3*,4,5,6,12,13,19,20,26,27"
},
{
"month": 12,
"days": "3,4,10,11,17,18,24,25,31"
}
];
const currentMonthDays = [];
const currentMonth = 11
const currentYear = 2022
const weekendsDays = weekendsJson[currentMonth - 1].days.split(',')
for (let i = 1; i <= 30; i++) {
currentMonthDays.push({ day: i, month: currentMonth, year: currentYear, weekendsDisabled: '?' });
}
Ответы (1 шт):
Автор решения: SwaD
→ Ссылка
Один из вариантов, воспользоваться методом includes и проверять список выходных дней с текущим элементом
const weekendsJson = [
{
"month": 1,
"days": "1,2,3,4,5,6,7,8,9,15,16,22,23,29,30"
},
{
"month": 2,
"days": "5,6,12,13,19,20,22*,23,26,27"
},
{
"month": 3,
"days": "5*,6,7+,8,12,13,19,20,26,27"
},
{
"month": 4,
"days": "2,3,9,10,16,17,23,24,30"
},
{
"month": 5,
"days": "1,2+,3+,7,8,9,10+,14,15,21,22,28,29"
},
{
"month": 6,
"days": "4,5,11,12,13+,18,19,25,26"
},
{
"month": 7,
"days": "2,3,9,10,16,17,23,24,30,31"
},
{
"month": 8,
"days": "6,7,13,14,20,21,27,28"
},
{
"month": 9,
"days": "3,4,10,11,17,18,24,25"
},
{
"month": 10,
"days": "1,2,8,9,15,16,22,23,29,30"
},
{
"month": 11,
"days": "3,4,5,6,12,13,19,20,26,27"
},
{
"month": 12,
"days": "3,4,10,11,17,18,24,25,31"
}
];
const currentMonthDays = [];
const currentMonth = 11
const currentYear = 2022
const weekendsDays = weekendsJson[currentMonth - 1].days.split(',');
for (let i = 1; i <= 30; i++) {
currentMonthDays.push({ day: i, month: currentMonth, year: currentYear, weekendsDisabled: weekendsDays.includes(i.toString()) });
}
console.log(currentMonthDays);
И обратите внимание, что не в каждом месяце 30 дней.