Problem :
Reformat the below array openingHours
which may be not a correct structure to have, but that's from where the problem is coming from..
If I do a directly JSON.parse(openingHours)[0].dayNo1
that would print 08:00 to 04:00
however in this case I need to iterate through the whole array where the key of each element ends by a number.
To iterate through each key, I've done the following :
let dayNo = JSON.parse(openingHours).map((el,index) => {
let workingHours = el.dayNo+index;
})
However, I'am obtaining a NaN
and I don't have an idea to go further with it..
The code:
var hoursArray = [];
var openingHours = [{
"type":"cafe",
"dayNo1": "08:00 to 04:00",
"dayNo2": "08:00 to 01:00",
"dayNo3": "08:00 to 12:00",
"dayNo4": "08:00 to 03:00",
"dayNo5": "08:00 to 19:00",
"dayNo6": "08:00 to 20:00",
"dayNo0": "08:00 to 23:00"
}]
//Get opening hours of days
let dayNo = JSON.parse(openingHours).map((el,index) => {
let workingHours = el.dayNo+index;
//Get opening hours of a particular day (for example "08:00 to 04:00" and render it to the following : ["08:00", "04:00"])
let startEndTime = workingHours.split(" to ");
//Final array format
hoursArray.push({
"dayNo":i,
"operatingHours":{
"opening": startEndTime[0],
"closing": startEndTime[1]
}
})
})