我写了一些用于过滤日期的代码。 在这里,我尝试使用返回布尔值创建一个过滤器,但是此代码仅适用于'item'的一个对象,而我需要过滤整个对象才能显示给网站目前仅提供工作时间表。 有人要求优化我编写的代码。
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script>
item = {
"work_schedule": {
"schedule": {
"MONDAY": {
"begin": "10:00",
"finish": "22:00"
},
"TUESDAY": {
"begin": "10:00",
"finish": "22:00"
},
"WEDNESDAY": {
"begin": "10:00",
"finish": "22:00"
},
"THURSDAY": {
"begin": "10:00",
"finish": "22:00"
},
"FRIDAY": {
"begin": "10:00",
"finish": "22:00"
}
}
},
"work_schedule": {
"schedule": {
"MONDAY": {
"begin": "10:00",
"finish": "22:00"
},
"TUESDAY": {
"begin": "10:00",
"finish": "22:00"
},
"WEDNESDAY": {
"begin": "10:00",
"finish": "22:00"
},
"THURSDAY": {
"begin": "10:00",
"finish": "22:00"
},
"FRIDAY": {
"begin": "10:00",
"finish": "22:00"
},
"SATURDAY": {
"begin": "10:00",
"finish": "22:00"
},
"SUNDAY": {
"begin": "10:00",
"finish": "22:00"
}
}
},
"work_schedule": {
"schedule": {
"MONDAY": {
"begin": "08:00",
"finish": "17:00"
},
"TUESDAY": {
"begin": "08:00",
"finish": "17:00"
},
"WEDNESDAY": {
"begin": "08:00",
"finish": "17:00"
},
"THURSDAY": {
"begin": "08:00",
"finish": "17:00"
},
"FRIDAY": {
"begin": "08:00",
"finish": "17:00"
}
}
},
}
const checkWorkDay = () => {
if (item?.work_schedule?.schedule) {
const dayOnWork = Object.keys(item?.work_schedule?.schedule).filter(
(el, index) => index == new Date().getDay() - 1
) // here I tried to filter one part of object I mean item. work_schedule.schedule
let time = null
switch (dayOnWork[0]) {
case 'MONDAY': {
time = item?.work_schedule?.schedule?.MONDAY
break
}
case 'TUESDAY': {
time = item?.work_schedule?.schedule?.TUESDAY
break
}
case 'WEDNESDAY': {
time = item?.work_schedule?.schedule?.WEDNESDAY
break
}
case 'THURSDAY': {
time = item?.work_schedule?.schedule?.THURSDAY
break
}
case 'FRIDAY': {
time = item?.work_schedule?.schedule?.FRIDAY
break
}
case 'SATURDAY': {
time = item?.work_schedule?.schedule?.SATURDAY
break
}
case 'SUNDAY': {
time = item?.work_schedule?.schedule?.SUNDAY
break
}
default: {
break
}
}
let beginTime = time?.begin.split(':')
let finishTime = time?.finish.split(':')
if (beginTime !== null && finishTime !== null) {
return (
beginTime[0] <= new Date().getHours() &&
finishTime[0] >= new Date().getHours()
)
}
}
}
答案 0 :(得分:0)
这是此问题的解决方案 首先,我们创建工作日数组以检查今天的工作日。 其次,我们使用func过滤器来制作数组,并使用破坏来获取进度表。 第三,我们检查数组中是否有今天的工作日(如果没有),则返回false。最后,我们在计划工作日内再次将其转换为数组,在该数组中开始和结束并将其拆分,并比较今天的工作时间是否过早,开放时间或关闭时间
const filtByWeekOpened = (items) => {
const days = [
'SUNDAY',
'MONDAY',
'TUESDAY',
'WEDNESDAY',
'THURSDAY',
'FRIDAY',
'SATURDAY'
]
const date = new Date()
const today = days[date.getDay()]
const time = date.getHours()
return items.filter(({ work_schedule: { schedule } }, idx) => {
if (!schedule.hasOwnProperty(today)) return false
return (
Number(schedule[today].begin.split(':')[0]) <=
time <=
Number(schedule[today].finish.split(':')[0])
)
})
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script>