0

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]
    }
  })

 })
Folky.H
  • 1,164
  • 4
  • 15
  • 37

2 Answers2

0

Your data format leaves some questions open. For example, openingHours is an array with a single object…will openingHours ever have more objects of other types?

If we assume that's not the case — that you are just trying to parse this one object — then a simple for loop is probably the clearest solution. This is a function that accepts a single object so you can pass it in with makeHoursObject(openingHours[0]). This will make it easier if you end up with more than one object in the array:

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

function makeHoursObject(obj){
    let hours = []
    for (let i = 0; i < 7; i++){
        let day = obj['dayNo'+i]
        if (day == undefined) continue // just in case there's no data for that day
        day = day.split(' to ') 
        hours.push({
            dayNo: i,
            operatingHours: {
             open: day[0], 
             close: day[1]
            }
        })
    }
    return hours
}

console.log(makeHoursObject(openingHours[0]))

You can also do this with reduce. I think it's a little less clear about the way it works, but might be a little more flexible:

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

function makeHoursObject(obj){
   return Object.entries(obj).reduce((a, [k, v]) => {
        let [_, dayNo] = k.split('dayNo')
        if (dayNo != null){
            day = v.split(' to ') 
            a.push({
                dayNo: dayNo,
                operatingHours: {
                 open: day[0], 
                 close: day[1]
                }
            })
        }
        return a
   }, [])
}

console.log(makeHoursObject(openingHours[0]))
Mark
  • 90,562
  • 7
  • 108
  • 148
-1

I made some small changes in your code, can you check if is working as you expected?

var hoursArray = [];

var openingHours = '{"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" }'


let x = JSON.parse(openingHours)
var arr = Object.values(x);
arr.map((el, index) => {
  let workingHours = el;

  //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": index,
    "operatingHours": {
      "opening": startEndTime[0],
      "closing": startEndTime[1]
    }
  })

});

https://stackblitz.com/edit/js-sxtcau

Tiago Machado
  • 170
  • 3
  • 11