gpt4 book ai didi

javascript - 使用 if 语句删除 Leaflet 弹出窗口中的 "null"属性

转载 作者:行者123 更新时间:2023-12-03 08:13:19 74 4
gpt4 key购买 nike

我正在使用外部 geojson 属性来填充 Leaflet 中的弹出窗口,如下所示:

function popUp (feature, geojson) {
var popupText=
"/*Name:*/" + feature.properties.name +
"/*Amount*/" + feature.properties.amounts;
geojson.bindPopup(popupText);
};

问题:某些金额为“空”。那么,如何编写 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);

关于javascript - 使用 if 语句删除 Leaflet 弹出窗口中的 "null"属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34055382/

74 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com