使用 moment JS 循环多个数组并转换值
我有一个函数,它接受一个对象,手动重新排序日期,并将字符串转换为
export const convertHoursOfOperation = (service) => {
const hours = {
Monday: service.hours_of_operation["monday"],
Tuesday: service.hours_of_operation["tuesday"],
Wednesday: service.hours_of_operation["wednesday"],
Thursday: service.hours_of_operation["thursday"],
Friday: service.hours_of_operation["friday"],
Saturday: service.hours_of_operation["saturday"],
Sunday: service.hours_of_operation["sunday"]
};
for (let hour in hours) {
hours[hour] !== null ? hours[hour] = hours[hour].split(",") : hours[hour] = 'Closed'
hours[hour] !== 'Closed' ? hours[hour].forEach((h) => moment(h, "HH:mm").format('h:mm')) : null
}
return hours;
}
hours[hour] !== null 的数组输出? hours[hour] = hours[hour].split(",") : hours[hour] = '休息'
星期一: '09:00, 15:00' =>;星期一:[09:00, 15:00]
或 星期一:null => monday: 'Closed'
然后我尝试循环遍历该数组(如果它是一个数组),并使用 moment
将值从军事时间转换为标准时间。
所以现在我想要的输出应该是:
monday: [09:00, 15:00] => monday: [9:00, 3:00]
当前行 hours[hour] !== 'Closed' ? hours[hour].forEach((h) => moment(h, "HH:mm").format('h:mm')) : null
不转换任何内容。我在这里做错了什么?
I have a function that takes an object, re-sorts the days manually, and converts a string to an array
export const convertHoursOfOperation = (service) => {
const hours = {
Monday: service.hours_of_operation["monday"],
Tuesday: service.hours_of_operation["tuesday"],
Wednesday: service.hours_of_operation["wednesday"],
Thursday: service.hours_of_operation["thursday"],
Friday: service.hours_of_operation["friday"],
Saturday: service.hours_of_operation["saturday"],
Sunday: service.hours_of_operation["sunday"]
};
for (let hour in hours) {
hours[hour] !== null ? hours[hour] = hours[hour].split(",") : hours[hour] = 'Closed'
hours[hour] !== 'Closed' ? hours[hour].forEach((h) => moment(h, "HH:mm").format('h:mm')) : null
}
return hours;
}
output from hours[hour] !== null ? hours[hour] = hours[hour].split(",") : hours[hour] = 'Closed'
monday: '09:00, 15:00' => monday: [09:00, 15:00]
OR monday: null => monday: 'Closed'
then I am trying to loop through the array if it is an array and convert the values from military to standard time using moment
.
so now my desired output should be:
monday: [09:00, 15:00] => monday: [9:00, 3:00]
currently the line hours[hour] !== 'Closed' ? hours[hour].forEach((h) => moment(h, "HH:mm").format('h:mm')) : null
is not converting anything. What am I doing wrong here?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您没有将您的
moment()
设置为任何内容。设置该特定索引处的值。正确的代码应该是这样的:you are not setting your
moment()
to anything. Set the value at that particular index. The correct code should be this: