字符串对象比较总是返回false

string objects comparison always returning false

本文关键字:返回 false 对象 比较 字符串      更新时间:2023-09-26

这是我正在执行的代码:

filterIssues: function(objectKey, text){
    var view = this;
    var keys = objectKey.split(".");
    var attributeKey = keys[0];
    var attributeName;
    if (keys.length > 1){
        attributeName = keys[1];
    }
    view.issues.each(function(issue){
        var value = issue.get(attributeKey);
        console.log(text);
        if (value === undefined || value === null){
            issue.trigger("hide");
            return;
        }
        if (attributeName !== undefined){
            value = value[attributeName];
        }
        if(value !== undefined){
            var matchedText = value.substring(0, text.length - 1);
            if ( matchedText === text){
                issue.trigger("show");
                console.log(value);  
                return;     
            }
        }
        issue.trigger("hide");
    });
}     

matchedText == text总是返回false

这是我玩控制台时得到的:

> matchedText
"sande"
> text
"sande"
> typeof(text)
"string"
> typeof(matchedText)
"string"
> matchedText === text
false
> matchedText == text
false

我确实意识到,===将始终检查两个对象是否相同,并且我已阅读JavaScript相等操作异常和JavaScript字符串相等。

我忽略的代码中有什么错误吗?

我认为您滥用了subString()方法。如果使用subString(),请使用不带-1的长度。

我最终发现了问题所在。谢谢你的回复,我相信你可能还没有找到答案,因为缺乏信息。

问题在于我传递给函数的text值。text在末尾包含一个"",这就是为什么比较不起作用的原因。