我正在尝试创建一个按钮,显示我按下按钮时打开的商店。我正在使用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
}
},
答案 0 :(得分:0)
变量hours[y]
不是字符串。
首先,您必须将for(y=0;i<hours.length;y++)
更改为for(var y in hours)
,因为您处理的object
没有属性length
。
下一步:
console.log(hours[y]);
返回:
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}
因此,如果您想要规范化任何时间,您必须单独执行,open
和close
小时。
for (var y in hours) {
var normalizeHoursOpen= hours[y].open.replace(/\s/g, '').toLowerCase();
var normalizeHoursClose= hours[y].close.replace(/\s/g, '').toLowerCase();
答案 1 :(得分:0)
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]])
}