仅在传单弹出窗口中显示填充的属性(而不是 "null"-属性)

Only show filled attributes in leaflet popups (and not "null"-attributes)

我的传单地图显示了来自 GeoJSON 文件的多边形。该文件有几个属性(attribute_1、attribute_2 等)。但是,有的是满文字,有的是空的。

如何在弹出窗口中只显示填充了文本的属性而不显示空属性?

使用我的代码显示在每个属性下方,如果它为空 "null" 则显示在弹出窗口中:

// Integrate GeoJSON and style polygons
  $.getJSON("klimagutachten_2001.geojson",function(klimagutachten){
    L.geoJson( klimagutachten, {
        style: function(feature){
            return {
                color: "#e60000",
                weight: 4,
                fillColor: "#e60000",
                fillOpacity: .3
            };
        },

// Call popup
        onEachFeature: function( feature, layer ){
            layer.bindPopup("<strong> Attribute 1: \"" + feature.properties.attribute_1 + " and the second attribute is: " + feature.properties.attribute_2)
        }                                                                                                       
    }).addTo(map);
  });

创建验证:

onEachFeature: function( feature, layer ){

var text = "";

if (!feature.properties.attribute_1) {
    text += feature.properties.attribute_1 +" "; 
}
if (!feature.properties.attribute_2) {
    text += feature.properties.attribute_2 +" "; 
}
layer.bindPopup(text);

} 

我用一个简单的 if...else 语句解决了这个问题。添加的内容以粗体突出显示:

// Integrate GeoJSON and style polygons
  $.getJSON("klimagutachten_2001.geojson",function(klimagutachten){
    L.geoJson( klimagutachten, {
        style: function(feature){
            return {
                color: "#e60000",
                weight: 4,
                fillColor: "#e60000",
                fillOpacity: .3
            };
        },

// Call popup
        onEachFeature: function( feature, layer ){
        var attribute_1 = feature.properties.attribute_1;
        if (attribute_1 == null) {
            attribute_1 = "";
        } else {
            var attribute_1 = feature.properties.attribute_1;
        };

            layer.bindPopup("<strong> Attribute 1: \"" + attribute_1 + " and the second attribute is: " + feature.properties.attribute_2)
        }   
    }).addTo(map);
  });