开关上无法访问变量

时间:2019-10-16 13:14:15

标签: javascript reactjs scope switch-statement

在另一个开关盒中不能访问变量startend。但应该是。

代码



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;
                }

1 个答案:

答案 0 :(得分:-1)

我发现了问题,这是由于startend放在case "dateVal"let start;上无法解决的在for循环之外。

像这样:

let end;

代替:

let start;
let end;
for (let key in filters) {
...
}