Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

119
Views
Looping through multiple arrays and converting values using moment JS

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?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

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;
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!