Javascript != "undefined" 上的 if 语句条件检查失败

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8357026/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 05:40:06  来源:igfitidea点击:

If-statement condition checking on != "undefined" fails

javascriptjqueryundefined

提问by

I'm trying to generate some HTML content for a google maps infowindow. I have 7 values which is supposed to be displayed if they do not equal null, undefined or "" (empty string).

我正在尝试为谷歌地图信息窗口生成一些 HTML 内容。我有 7 个值,如果它们不等于 null、undefined 或 ""(空字符串),则应该显示这些值。

But apparently my if(e.Property != null || e.Property != "undefined" || e.Property == "")doesn't work when a Propertyis undefined. Mostly the case is that e.Emailis undefined. So instead of skipping that part, my code still inserts the html + "<br />part. And when I alert()the e.Email it returns undefinedwhich it's supposed to catch and skip if that was the case.

但显然if(e.Property != null || e.Property != "undefined" || e.Property == "")当 a Propertyis时我的不起作用undefined。大多数情况下e.Email是未定义的。因此,我的代码仍然会插入该html + "<br />部分,而不是跳过该部分。当我alert()发送电子邮件时,它会返回undefined它应该捕获并跳过的内容(如果是这种情况)。

I have tried writting if(typeof e.Property != null || typeof e.Property != "undefined" || typeof e.Property == ""), but that made no difference.

我试过写作if(typeof e.Property != null || typeof e.Property != "undefined" || typeof e.Property == ""),但这没有什么区别。

// 'e ' is JSON object
var generateHTML = {
    init: function(e) {
        if (e != null || e != "undefined"){
            generateHTML.check(e);
        }
    },
    check: function (e) {
        if(e.Title != null || e.Title != "undefined" || e.Title == ""){
            html = html + "<b>"+e.Title+"</b>";
        }
        if(e.Address != null || e.Address != "undefined" || e.Address == ""){
            html = html +"<br />"+ e.Address;
        }
        if(e.Zipcode != null || e.Zipcode != "undefined" || e.Zipcode == ""){
            html = html +"<br />"+ e.Zipcode+", ";
        }
        if(e.City != null || e.City != "undefined" || e.City == ""){
            html = html + e.City;
        }
        if(e.Phone != null || e.Phone != "undefined" || e.Phone == ""){
            html = html +"<br />"+ e.Phone;
        }
        if(e.Email != null || e.Email != "undefined" || e.Email == ""){
            html = html +"<br />"+ e.Email;
        }
        if(e.WebAddress != null || e.WebAddress != "undefined" || e.WebAddress == ""){
            html = html +"<br />"+ e.WebAddress;
        }
        return html;
    }
};

采纳答案by jabclab

If you want a more shorthand version you can just use:

如果你想要一个更简写的版本,你可以使用:

if (e.Title) {
    // add to HTML
}
if (e.Address) {
    // add to HTML
}

You may want to consider building your HTML as an Array and then joining at the end to avoid creating many strings, e.g.

您可能需要考虑将 HTML 构建为一个数组,然后在最后加入以避免创建许多字符串,例如

var html = [];
html.push("FirstName");
html.push("<br />");
html.push("LastName");
html.push("<br />");
html.push("Number");
var output = html.join(""); // "FirstName<br />LastName<br />Number"

回答by psynnott

You want to check for !== undefined

你想检查 !== undefined

e.g.

例如

if(myvar !== undefined) { 
    //DO SOMETHING 
}

回答by Brook

if(e) //this would be shorter

if(e != undefined)
//
if(typeof(e) != 'undefined')

回答by Richard Dalton

undefinedis a variable name, not a string.

undefined是变量名,不是字符串。

You don't need the quotes around it.

你不需要它周围的引号。

回答by Kemal Can Kara

You are checking it as if its value is string "undefined"

您正在检查它,就好像它的值是字符串“未定义”

remove the ""

去除那个 ””

回答by vonsko

better check something via e.length cause variables type are not acurate in JavaScript

最好通过 e.length 检查一些东西,因为 JavaScript 中的变量类型不准确

回答by Colin Jameson

I would also use the length function, if the array or object is empty the Logged length will be 0.0, i.e.

我也会使用长度函数,如果数组或对象为空,记录长度将为 0.0,即

if(e.length == 0){
  //then do something or nothing
}
else {
  //Do somthing
}