0

I am trying to create a button that displays what shops are open at the time I push the button. I am using filterJSON: Function() to filter out the times that are open but I can't seem to get the loop to display just the open times. I know what I need to do to create the button but can't figure out a way to just display open times at the the current time.

    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 is below

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
    }
},
iH8
  • 27,722
  • 4
  • 67
  • 76
Laura
  • 3
  • 3
  • possible duplicate of [JavaScript for...in vs for](http://stackoverflow.com/questions/242841/javascript-for-in-vs-for) – Andreas Louv Mar 06 '15 at 16:59
  • Edited your question to remove the reference and tag pointing to GeoJSON because this has absolutely nothing to do with GeoJSON. Added JSON tag, that's what you're working with. See: http://geojson.org/ – iH8 Mar 06 '15 at 17:05
  • @iH8 Looks like the question has [nothing to do with JSON](http://timelessrepo.com/json-isnt-a-javascript-subset) either ; ). – Teemu Mar 06 '15 at 17:11

2 Answers2

0

Variable hours[y] is not a string.

First You must change Your for(y=0;i<hours.length;y++) to for(var y in hours) because u working on object which not have property length.

Next:

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}

So if You want normalize any hours, You must do it separately, for open and close hours.

for (var y in hours) {

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

JSFiddle

enter image description here

Mateusz Mania
  • 839
  • 4
  • 15
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]])
  }
Yene Mulatu
  • 2,226
  • 1
  • 17
  • 13