2
function makeHtml(response) {
  $.each(response.workers,function(i){ 
    $.each(response.dutchDays,function(j) {
      var dayFrom = 'day'+j+'from';
        console.log(response.workers[i].dayFrom); // This is showing me undefined
    }
}
dutchDays:{1: "Maandag:", 2: "Dinsdag:", 3: "Woensdag:", 4: "Donderdag:", 5: "Vrijdag:", 6: "Zaterdag:",…}
timeArray:["00:00:00", "00:30:00", "01:00:00", "01:30:00", "02:00:00", "02:30:00", "03:00:00", "03:30:00",…]
workers:[{"workerid":"51","barberid":"41","name":"Mo","workerEmail":"nkm_mootje@hotmail.com","worker_time_id":"57","time_needed":"30","day1from":"12:00:00","day1to":"18:00:00","day2from":"09:00:00","day2to":"18:00:00","day3from":"09:00:00","day3to":"18:00:00","day4from":"09:00:00","day4to":"20:30:00","day5from":"09:00:00","day5to":"18:00:00","day6from":"09:00:00","day6to":"18:00:00","day7from":"05:00:00","day7to":"12:30:00"}]

This is my function in which i want to use dynamic day1from and day1to , day2from and day2to... from workers json array in jquery each loop. it shows me undefined.

Nirav Joshi
  • 2,924
  • 1
  • 23
  • 45

1 Answers1

1

There was a couple of syntax errors I corrected, but besides that, you are looking for the index from the variable dayFrom not the index dayFrom.

function makeHtml(response) {
  $.each(response.workers,function(i){ 
    $.each(response.dutchDays,function(j) {
      var dayFrom = 'day'+j+'from';
      console.log(response.workers[i][dayFrom]); // This is showing me undefined
    });
  });
}
response= {
  dutchDays:{1: "Maandag:", 2: "Dinsdag:", 3: "Woensdag:", 4: "Donderdag:", 5: "Vrijdag:", 6: "Zaterdag:"},
  timeArray:["00:00:00", "00:30:00", "01:00:00", "01:30:00", "02:00:00", "02:30:00", "03:00:00", "03:30:00"],
  workers:[{"workerid":"51","barberid":"41","name":"Mo","workerEmail":"nkm_mootje@hotmail.com","worker_time_id":"57","time_needed":"30","day1from":"12:00:00","day1to":"18:00:00","day2from":"09:00:00","day2to":"18:00:00","day3from":"09:00:00","day3to":"18:00:00","day4from":"09:00:00","day4to":"20:30:00","day5from":"09:00:00","day5to":"18:00:00","day6from":"09:00:00","day6to":"18:00:00","day7from":"05:00:00","day7to":"12:30:00"}]
}


makeHtml(response);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
Neil
  • 14,063
  • 3
  • 30
  • 51