在另一个开关盒中不能访问变量start
和end
。但应该是。
代码
for (let key in filters) {
let obj = filters[key];
if (!filters.hasOwnProperty(key) || obj === '' || obj.length === 0) {
continue;
}
let start;
let end;
switch (key) {
case "datum_van": {
start = new Date(obj);
break;
}
case "datum_tot": {
end = new Date(obj);
break;
}
case "dateVal": {
filteredRoutes = filteredRoutes.filter(tocht => {
let date = this.parseJsonDate(tocht.datum_van);
switch (obj) {
case 0: return date >= start && date <= end;
case 1: return date >= start;
case 2: return date <= end;
}
});
break;
}
答案 0 :(得分:-1)
我发现了问题,这是由于start
和end
放在case "dateVal"
和let start;
上无法解决的在for循环之外。
像这样:
let end;
代替:
let start;
let end;
for (let key in filters) {
...
}