This question already has answers here:
Dynamically access object property using variable
(13个回答)
2年前关闭。
这是我的功能,我想在每个循环的jquery中使用工人json数组中的动态day1from和day1to,day2from和day2to ...。它告诉我未定义。
(13个回答)
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":"[email protected]","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"}]
这是我的功能,我想在每个循环的jquery中使用工人json数组中的动态day1from和day1to,day2from和day2to ...。它告诉我未定义。
最佳答案
我纠正了一些语法错误,但除此之外,您还从变量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":"[email protected]","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>
10-07 12:55