sushibox sushibox - 17 days ago 5
jQuery Question

jquery print object content

I'm new to jquery and want to print all times from monday only (or day == today later on)

I manage to print all times of the object and not 1 day I pick, maybe my iteration is wrong, would like any hints.



var arr = [{
"dateString": "2016-12-05",
"weekday": "mon",
"opening_hours": [{
"from": "09:00",
"to": "15:00",
"fromNumber": 9,
"toNumber": 15
}, {
"from": "17:00",
"to": "20:00",
"fromNumber": 17,
"toNumber": 20
}]
}, {
"dateString": "2016-12-06",
"weekday": "tue",
"opening_hours": [{
"from": "09:00",
"to": "17:00",
"fromNumber": 9,
"toNumber": 17
}]
}, {
"dateString": "2016-12-07",
"weekday": "wed",
"opening_hours": [{
"from": "09:00",
"to": "20:00",
"fromNumber": 9,
"toNumber": 20
}]
}, {
"dateString": "2016-12-08",
"weekday": "thu",
"opening_hours": [{
"from": "1:00",
"to": "11:00",
"fromNumber": 1,
"toNumber": 11
}]
}];

$("button").click(function() {

var today = new Date();
var str = today.toString('dddd, MMMM ,yyyy')
var sta = str.substring(0, 4);
console.log(sta);
$.each(arr, function(i, currProgram) {
$.each(currProgram.opening_hours, function(key, val) {
// $.each(num, function (axa, dasa) {
if (currProgram.weekday == "mon") {
console.log("monday");
timefrom = (val.from);
timeto = (val.to);
} else {
console.log("no monday");
}
// console.log(num[axa]);
$(".result").append("\ntime: " + timefrom + " to " + timeto + "\n");
console.log("time: " + timefrom + " to " + timeto + "\n");
// });
});
});
});

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<p class="calnote">
<day></day>
<br>
<month></month>
<blackcorner>
<em>
<time></time>
</em>
</blackcorner>
</p>

<div id="div1">
<h2>click button for ajax</h2></div>
<div class="result">
<br>
</div>result</div>
<br>
<div class="result1">

</div>result1</div>
<p class="calnote">
<day></day>
<br>
<month></month>

<blackcorner>
<em>
<time></time>
</em>
</blackcorner>
</p>
click event
<button class="abc">Click me</button> here





jsfiddle

The result should print only:


time: 09:00 to 15:00

time: 17:00 to 20:00

Answer

You might want to move the logic console.log("time: " + timefrom + " to " + timeto + "\n"); into the if block

var arr = [{
  "dateString": "2016-12-05",
  "weekday": "mon",
  "opening_hours": [{
    "from": "09:00",
    "to": "15:00",
    "fromNumber": 9,
    "toNumber": 15
  }, {
    "from": "17:00",
    "to": "20:00",
    "fromNumber": 17,
    "toNumber": 20
  }]
}, {
  "dateString": "2016-12-06",
  "weekday": "tue",
  "opening_hours": [{
    "from": "09:00",
    "to": "17:00",
    "fromNumber": 9,
    "toNumber": 17
  }]
}, {
  "dateString": "2016-12-07",
  "weekday": "wed",
  "opening_hours": [{
    "from": "09:00",
    "to": "20:00",
    "fromNumber": 9,
    "toNumber": 20
  }]
}, {
  "dateString": "2016-12-08",
  "weekday": "thu",
  "opening_hours": [{
    "from": "1:00",
    "to": "11:00",
    "fromNumber": 1,
    "toNumber": 11
  }]
}];

function buttonClick() {

  var today = new Date();
  var str = today.toString('dddd, MMMM ,yyyy')
  var sta = str.substring(0, 4); 
  $.each(arr, function(i, currProgram) {
    $.each(currProgram.opening_hours, function(key, val) {      
      if (currProgram.weekday == "mon") {
        //console.log("monday");
        timefrom = (val.from);
        timeto = (val.to);       
      	console.log("time: " + timefrom + " to " + timeto + "\n");      
      } else {
        //console.log("no monday");
      }      
    });
  });
}

buttonClick()
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>