代码之家  ›  专栏  ›  技术社区  ›  DaveF

Javscript Switch表达式-如何使其不区分大小写?

  •  -1
  • DaveF  · 技术社区  · 2 年前

    我有两个geojson文件,它们的键/标签相同,但大小写不同: feature.properties.STATUS feature.properties.status

    我希望用一个switch语句测试两个文件标签的值:

        function PathStyle(feature) {
            switch (feature.properties.STATUS) {
                case "FP": 
                    return {color: 'blue'}; 
                case "BR": 
                    return {color: 'green'};
                    etc...
                    
    

    这只会产生大写STATUS值的预期结果。

    我试过 .toLowerCase() 方法多种多样,但都没有成功。即:

        function PathStyle(feature) {
         var str = "feature.properties.STATUS";
         var strLC = str.toLowerCase();
          console.log(strLC);
            switch (strLC) {
            etc...
    

    这个问题有解决办法吗?

    1 回复  |  直到 2 年前
        1
  •  2
  •   Barmar    2 年前

    使用null合并运算符同时尝试 STATUS status ,以实际存在的为准。

        function PathStyle(feature) {
            switch (feature.properties.STATUS ?? feature.properties.status) {
                case "FP": 
                    return {color: 'blue'}; 
                case "BR": 
                    return {color: 'green'};
                    etc...