切换语句Java脚本

时间:2018-07-03 20:27:31

标签: javascript switch-statement

尝试将颜色分配给各种多边形。我不确定我是否正确执行了此操作。我使用的是if else语句,但同事建议使用switch语句。我无法获取下面的代码来工作。

我是Java语言的新手,但我想尝试在传单上制作地图。我开始遵循在这里找到的教程:maptimeboston.github.io/leaflet-intro。我一直在潜入自己的数据。我发现当我在本教程中使用else if语句时,所有多边形都被分配了第一个分配的颜色

$.getJSON("SA_Access.geojson", function(accesspolys) {
  L.geoJson(accesspolys, {
    style: function(feature) {
      var fillColor,
        servicelayer = feature.properties.POLYID;
      switch (servicelayer) {
        case '10987':
          fillColor = "#006837";
          break;
        case '10988':
          fillcolor = "#31a354";
          break;
        default:
          fillColor = "#FFFFFF"
      }
    }
    onEachFeature: function(feature, layer) {
      layer.bindPopup("<strong>" + feature.properties.POLYNAME)
    }
  }).addTo(map);
});

最初,我的代码如下:

$.getJSON("SA_Access.geojson",function(accesspolys){
  L.geoJson( accesspolys, {
      style: function(feature){
        var fillColor,
            servicelayer = feature.properties.POLYID;
        if (servicelayer = '10987' ) fillColor = "#006837";
        else if ( servicelayer = '10988' ) fillColor = "#31a354";
        else if ( servicelayer = '10989' ) fillColor = "#78c679";
        else if ( servicelayer = '10990' ) fillColor = "#c2e699";
        else if ( servicelayer = '10991' ) fillColor = "#800000";
        else if ( servicelayer = '10992' ) fillColor = "#FFFF00";
        else if ( servicelayer = '10993' ) fillColor = "#00FF00";
        else if ( servicelayer = '10994' ) fillColor = "#808000";
        else if ( servicelayer = '10995' ) fillColor = "#ff6347";
        else if ( servicelayer = '10996' ) fillColor = "#ee82ee";
        else if ( servicelayer = '10997' ) fillColor = "#6a5acd";
        else if ( servicelayer = '10998' ) fillColor = "#ffa500";
        else if ( servicelayer = '10999' ) fillColor = "#3cb371";
        else if ( servicelayer = '11100' ) fillColor = "#87554A";
        else if ( servicelayer = '11101' ) fillColor = "#685ab3";
        else if ( servicelayer = '11102' ) fillColor = "#2b42d9";
        else if ( servicelayer = '11103' ) fillColor = "#d92bb4";
        else if ( servicelayer = '11152' ) fillColor = "#982880";
        else if ( servicelayer = '11153' ) fillColor = "#459828";
        else if ( servicelayer = '11154' ) fillColor = "#288398";
        else if ( servicelayer = '11155' ) fillColor = "#982891";
        else if ( servicelayer = '11156' ) fillColor = "#284e98";
        else if ( servicelayer = '11157' ) fillColor = "#982860";
        else if ( servicelayer = '11158' ) fillColor = "#989428";
        else if ( servicelayer = '11159' ) fillColor = "#362898";
        else fillColor = "#f7f7f7";  // no data
        return { color: "#999", weight: 1, fillColor: fillColor, fillOpacity: .6 };
      },
      onEachFeature: function( feature, layer ){
        layer.bindPopup( "<strong>" + feature.properties.POLYNAME)
      }
    }).addTo(map);
  });

2 个答案:

答案 0 :(得分:1)

只需创建一个可以在其中查找功能的地图即可。在没有任何点击的情况下,它变成undefined,因此您可以使用||来产生默认值。

const colorMap = {
    10987: '#006837', 
    10988: '#31a354', 
    10989: '#78c679', 
    // etc
};

function getColor(serviceLayer) {
  return colorMap[serviceLayer] || '#f7f7f7';
}


getColor(999);
// => '#f7f7f7'
getColor(10988);
// => '#31a354'

答案 1 :(得分:0)

鉴于您要获取一个值,并且在这种情况下输出其他值,您最好使用某种键值字典模式。

最简单的实现是使用多维数组,例如

var items = [
    [10987, "#31a354"],
    [10989, "#78c679"],
    [10990, "#c2e699"]
];

然后编写一个简单的函数来匹配元素0(即10987)中的值并返回元素1(即“#31a354”)

function getValue(dict, key)
    for(var i = 0; i < dict.length; i++)
        if(dict[i][0] == key){
            return dict[i][1];
        }
    }
    return "somedefaultvalue";
}

更结构化的方法可能是组装一个json对象,例如:

var items = [
    {"key": 10987, "value": "#31a354"},
    {"key": 10989, "value": "#78c679"},
    {"key": 10990, "value": "#c2e699"},
];

可以肯定,这看上去很相似,但是当您进行迭代而不是仅将元素引用为item [0] [1]时,您会拥有item [0] .key。

function getValue(dict, key)
    for(var i = 0; i < dict.length; i++)
        if(dict[i].key == key){
            return dict[i].value;
        }
    }
    return "somedefaultvalue";
}

在两种情况下,您都将像这样调用getValue,我一直假设“键”是整数数据类型-可以很容易地将其转换为字符串或任何需要的东西。

var fillColor = getValue(items, 10987);

**道歉,写了所有这些徒手画,所以可能需要一些整理。希望对您有帮助!

编辑:如果您想扩展应用程序以从外部文件或某种类型的服务中读取值,那么json方法非常有用,在这种情况下,这是很正常的做法。