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

142
Views
Separate array after each second comma

I got an array from a CMS that is poorly formatted which I want to reformat to an array of objects.

let daysOffArray = ["2022-08-22, 08:00 - 14:00","2022-08-23, 08:00 - 13:00"];

Expected result after separating the array:

let daysOff = [{
date: "2022-08-22",
time: 08:00 - 14:00
},
{
date: "2022-08-23",
time: 08:00 - 13:00
}];

How can I separate the array daysOffArray for each second comma and then separate the two new arrays after each comma?

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

0

Use String#split and Array#map:

let daysOff = daysOffArray.map(i => {
  const data = i.split(", "):
  return { date: data[0], time: data[1] };
});
about 4 years ago · Juan Pablo Isaza Report

0

let daysOffArray = ["2022-08-22, 08:00 - 14:00","2022-08-23, 08:00 - 13:00"];

let daysOff=[]

daysOffArray.map((item)=>{
daysOff.push({date:item.split(",")[0],time:item.split(",")[1]})
})

console.log(daysOff)
about 4 years ago · Juan Pablo Isaza Report

0

  // Initial object
  var daysOffArray = [
     '2022-08-22, 08:00 - 14:00',
     '2022-08-23, 08:00 - 13:00',
  ];

  // An empty array for the formatted object
  var daysOffFormatted = [];

  // Splitting and formatting the array
  daysOffArray.forEach((daysOff) => {
     var date = daysOff.split(',')[0].trim();
     var time = daysOff.split(',')[1].trim();

     // Adding formatted object to the array

     daysOffFormatted.push({ date, time });
  });

  console.log(daysOffFormatted);
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!