遍历多个数组并使用 moment JS 转换值
Looping through multiple arrays and converting values using 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] = 'Closed'
的输出
monday: '09:00, 15:00' => monday: [09:00, 15:00]
或 monday: 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
行没有转换任何东西。我在这里做错了什么?
您没有将 moment()
设置为任何内容。设置该特定索引处的值。正确的代码应该是这样的:
hours[hour] !== 'Closed' ? hours[hour].forEach((h,index) => hours[hour][index] = moment(h, "HH:mm").format('h:mm')) : null;
我有一个函数,它接受一个对象,手动重新排序日期,并将字符串转换为数组
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] = 'Closed'
monday: '09:00, 15:00' => monday: [09:00, 15:00]
或 monday: 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
行没有转换任何东西。我在这里做错了什么?
您没有将 moment()
设置为任何内容。设置该特定索引处的值。正确的代码应该是这样的:
hours[hour] !== 'Closed' ? hours[hour].forEach((h,index) => hours[hour][index] = moment(h, "HH:mm").format('h:mm')) : null;