Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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 for each second comma and then separate the two new arrays after each comma?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading