在获取json数据时遇到jquery问题

时间:2017-06-02 15:58:09

标签: javascript jquery json

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"}]

这是我的函数,其中我想使用动态day1from和day1to,day2from和day2to ...来自jquery每个循环中的worker json数组。它显示我未定义。

1 个答案:

答案 0 :(得分:1)

我纠正了一些语法错误,但除此之外,您正在寻找变量dayFrom而非索引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>
&#13;
&#13;
&#13;