使用 if 语句删除 Leaflet 弹出窗口中的 "null" 属性
Remove "null" attributes in Leaflet popups with an if-statement
我正在使用外部 geojson 属性来填充 Leaflet 中的弹出窗口 windows,如下所示:
function popUp (feature, geojson) {
var popupText=
"/*Name:*/" + feature.properties.name +
"/*Amount*/" + feature.properties.amounts;
geojson.bindPopup(popupText);
};
问题:有些金额是 "null"。那么,我该如何编写 if 语句,以便如果金额为 "null",则字符串 ("Amount") 和属性 ("null") 都不会出现在弹出窗口中?
您要查找的是条件语句:
https://developer.mozilla.org/en-US/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);
或者使用条件三元运算符甚至更短:
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);
我正在使用外部 geojson 属性来填充 Leaflet 中的弹出窗口 windows,如下所示:
function popUp (feature, geojson) {
var popupText=
"/*Name:*/" + feature.properties.name +
"/*Amount*/" + feature.properties.amounts;
geojson.bindPopup(popupText);
};
问题:有些金额是 "null"。那么,我该如何编写 if 语句,以便如果金额为 "null",则字符串 ("Amount") 和属性 ("null") 都不会出现在弹出窗口中?
您要查找的是条件语句:
https://developer.mozilla.org/en-US/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);
或者使用条件三元运算符甚至更短:
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);