Javascript - 迭代和数组,并重新排序其元素

时间:2018-04-29 19:03:41

标签: javascript

问题:

重新格式化下面的数组openingHours,这可能不是一个正确的结构,但那是问题来自的地方..

如果我直接JSON.parse(openingHours)[0].dayNo1会打印08:00 to 04:00但是在这种情况下,我需要遍历整个数组,其中每个元素的键以数字结尾。

要遍历每个键,我已完成以下操作:

    let dayNo = JSON.parse(openingHours).map((el,index) => {
    let workingHours = el.dayNo+index;
    })

然而,我获得NaN并且我没有进一步了解它的想法..

代码:

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

 })

2 个答案:

答案 0 :(得分:0)

您的数据格式会留下一些问题。例如,openingHours是一个包含单个对象的数组... openingHours会有更多其他types的对象吗?

如果我们假设情况并非如此 - 您只是试图解析这一个对象 - 那么简单的for循环可能是最清晰的解决方案。这是一个接受单个对象的函数,因此您可以使用makeHoursObject(openingHours[0])传递它。如果最终在数组中有多个对象,这将使其更容易:



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]))
&#13;
&#13;
&#13;

您也可以使用reduce执行此操作。我认为它的工作方式不太清楚,但可能会更灵活一些:

&#13;
&#13;
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]))
&#13;
&#13;
&#13;

答案 1 :(得分:-1)

我在代码中做了一些小改动,你能检查一下是否按预期工作了吗?

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