现在打开时间按钮

Open now time button

我正在尝试创建一个按钮,在我按下按钮时显示哪些商店正在营业。我正在使用 filterJSON: Function() 来过滤掉开放时间,但我似乎无法让循环只显示开放时间。我知道我需要做什么来创建按钮,但想不出一种方法来只显示当前时间的开放时间。

    var hours = feature.properties.hours;
      for (y = 0; y < hours.length; y++) {
        var normalizeHours= hours[y].replace(/\s/g, '').toLowerCase();
        if (watcher.indexOf(normalizeHours) !== -1) {
          return feature;
        }
      }`

JSON低于

hours: {
    monday: {
        close: "18:00:00",
        open: "10:00:00",
        call: false
    },
    tuesday: {
        close: "18:00:00",
        open: "12:00:00",
        call: true
    },
    friday: {
        close: "None",
        open: "None",
        call: false
    },
    wednesday: {
        close: "18:00:00",
        open: "17:00:00",
        call: false
    },
    thursday: {
        close: "None",
        open: "None",
        call: true
    },
    sunday: {
        close: "18:00:00",
        open: "15:33:00",
        call: false
    },
    saturday: {
        close: "18:00:00",
        open: "15:00:00",
        call: false
    }
},

变量hours[y]不是字符串。

首先,您必须将 for(y=0;i<hours.length;y++) 更改为 for(var y in hours),因为您正在处理 object 而没有 属性 length.

下一个:

console.log(hours[y]);

Returns:

Object {close: "18:00:00", open: "10:00:00", call: false}
Object {close: "18:00:00", open: "12:00:00", call: true}
Object {close: "None", open: "None", call: false}
Object {close: "18:00:00", open: "17:00:00", call: false}
Object {close: "None", open: "None", call: true}
Object {close: "18:00:00", open: "15:33:00", call: false}
Object {close: "18:00:00", open: "15:00:00", call: false}

因此,如果您想标准化任何时间,则必须分别进行 openclose 小时。

for (var y in hours) {

    var normalizeHoursOpen= hours[y].open.replace(/\s/g, '').toLowerCase();
    var normalizeHoursClose= hours[y].close.replace(/\s/g, '').toLowerCase();

JSFiddle

   var week = ["monday","tuesday","wednesday","thursday","saturday","sunday","friday"];

  var hours =  {
    monday: {
        close: "18:00:00",
        open: "10:00:00",
        call: false
    },
    tuesday: {
        close: "18:00:00",
        open: "12:00:00",
        call: true
    },
    friday: {
        close: "None",
        open: "None",
        call: false
    },
    wednesday: {
        close: "18:00:00",
        open: "17:00:00",
        call: false
    },
    thursday: {
        close: "None",
        open: "None",
        call: true
    },
    sunday: {
        close: "18:00:00",
        open: "15:33:00",
        call: false
    },
    saturday: {
        close: "18:00:00",
        open: "15:00:00",
        call: false
    }
}


  for (day in week){

        console.log(hours[week[day]])
  }