删除"null"带有if语句的Leaflet弹出窗口中的属性 [英] Remove "null" attributes in Leaflet popups with an if-statement
问题描述
我正在使用外部geojson属性来填充Leaflet中的弹出窗口,如下所示:
I am using external geojson attributes to fill my popup windows in Leaflet, like this:
function popUp (feature, geojson) {
var popupText=
"/*Name:*/" + feature.properties.name +
"/*Amount*/" + feature.properties.amounts;
geojson.bindPopup(popupText);
};
问题:某些金额为"null".那么,如何编写if语句,以便如果金额为"null",则字符串("Amount")和属性("null")都不会显示在弹出窗口中?
The problem: some of the amounts are "null". So, how do I write an if statement, so that if an amount is "null" then both the string ("Amount") and the attribute ("null") don't show up in the popup?
推荐答案
您要查找的是条件语句:
What you are looking for is a conditional statement:
https://developer. mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Statements/if...else
var popupText = '';
if (feature.properties.name) {
popupText += '/*Name:*/' + feature.properties.name;
}
if (feature.properties.amount) {
popupText += '/*Amount*/' + feature.properties.amount;
}
geojson.bindPopup(popupText);
或更短地使用条件三元运算符:
Or even shorter using a conditional ternary operator:
https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Operators/Conditional_Operator
var popupText = '';
popupText += (feature.properties.name) ? '/*Name:*/' + feature.properties.name : '';
popupText += (feature.properties.amount) ? '/*Amount*/' + feature.properties.amount : '';
geojson.bindPopup(popupText);
这篇关于删除"null"带有if语句的Leaflet弹出窗口中的属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!