使用 moment JS 循环多个数组并转换值

发布于 2025-01-16 23:05:34 字数 1299 浏览 1 评论 0原文

我有一个函数,它接受一个对象,手动重新排序日期,并将字符串转换为

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

孤星 2025-01-23 23:05:34

您没有将您的 moment() 设置为任何内容。设置该特定索引处的值。正确的代码应该是这样的:

hours[hour] !== 'Closed' ? hours[hour].forEach((h,index) => hours[hour][index] = moment(h, "HH:mm").format('h:mm')) : null;

you are not setting your moment() to anything. Set the value at that particular index. The correct code should be this:

hours[hour] !== 'Closed' ? hours[hour].forEach((h,index) => hours[hour][index] = moment(h, "HH:mm").format('h:mm')) : null;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文