Jquery problems when getting json data

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 where I want to use dynamic day1from and day1to, day2from and day2to ... from working json array to jquery every loop. it shows me undefined.

+3


source to share


1 answer


Several syntax errors have been fixed, but other than that, you are looking for an index from a variable dayFrom

, not an 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>
      

Run code


+1


source







All Articles