我想循环一个数组,但是不幸的是我的for循环只执行1次。
这是我的代码:
var cmpz_disabled_dates=$.parseJSON(disabled_dates);
var date = new Date();
console.log(cmpz_disabled_dates);
for (i = 0; i < cmpz_disabled_dates.length; i++) {
var type = cmpz_disabled_dates[i]["type"];
if(type === "ebdida_day") {
var disabled_day = Number(cmpz_disabled_dates[i]["disabled"]) + 1;
if(disabled_day == 8) {
disabled_day = 0;
}
var day = date.getDay();
console.log(i,date,day,disabled_day);
return [(disabled_day != day)];
}
}
console.log的输出(cmpz_disabled_dates);
0: {type: "ebdida_day", disabled: "7", repeat: "off", calendar: "start_end"}
1: {type: "ebdida_date_range", disabled: Array(2), repeat: "on", calendar: "start_end"}
2: {type: "ebdida_date", disabled: "2020-04-10", repeat: "off", calendar: "start_end"}
3: {type: "ebdida_date", disabled: "2020-04-13", repeat: "off", calendar: "start_end"}
4: {type: "ebdida_date_range", disabled: Array(2), repeat: "off", calendar: "start_end"}
5: {type: "ebdida_date", disabled: "2020-05-01", repeat: "off", calendar: "start_end"}
6: {type: "ebdida_date", disabled: "2020-05-21", repeat: "off", calendar: "start_end"}
7: {type: "ebdida_date", disabled: "2020-06-01", repeat: "off", calendar: "start_end"}
8: {type: "ebdida_date_range", disabled: Array(2), repeat: "off", calendar: "start_end"}
9: {type: "ebdida_date_range", disabled: Array(2), repeat: "off", calendar: "start_end"}
10: {type: "ebdida_date_range", disabled: Array(2), repeat: "off", calendar: "start_end"}
11: {type: "ebdida_date_range", disabled: Array(2), repeat: "off", calendar: "start_end"}
12: {type: "ebdida_date_range", disabled: Array(2), repeat: "off", calendar: "start_end"}
13: {type: "ebdida_day", disabled: "3", repeat: "off", calendar: "start_end"}
14: {type: "ebdida_day", disabled: "6", repeat: "off", calendar: "start_end"}
length: 15
console.log(i,date,day,disabled_day);
0 Wed Sep 23 2020 08:48:48 GMT+0200 (Mitteleuropäische Sommerzeit) 3 0
->因此,仅输出元素0,不输出元素13和14(它们也来自“ ebdida_day”类型)
我认为,for循环正在覆盖某些内容,但我找不到该错误。谢谢!
答案 0 :(得分:1)
您有一个return
,它将中断迭代。
答案 1 :(得分:1)
在您的for循环中删除return语句
答案 2 :(得分:1)
在for循环块外设置return语句,否则迭代会停止
var cmpz_disabled_dates=$.parseJSON(disabled_dates);
var date = new Date();
console.log(cmpz_disabled_dates);
for (i = 0; i < cmpz_disabled_dates.length; i++) {
var type = cmpz_disabled_dates[i]["type"];
if(type === "ebdida_day") {
var disabled_day = Number(cmpz_disabled_dates[i]["disabled"]) + 1;
if(disabled_day == 8) {
disabled_day = 0;
}
var day = date.getDay();
console.log(i,date,day,disabled_day);
}
return [(disabled_day != day)];
}