String对象的比较总是返回false

问题描述:

这里是我执行的代码:String对象的比较总是返回false

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 equal operations anomaliesJavascript string equality

我忽略的代码有什么问题吗?

+0

检查'matchedText.length'和'text.length'。 –

+0

什么样的对象是'issues',什么是'issues.each()'传递给函数(即什么是'问题')? 'issue.get()'返回什么? – RobG

+0

也许栅栏发布错误? 'var matchedText = value.substring(0,text.length - 1);'你尝试没有 - 1? 'matchedText.valueOf()=== text.valueOf()'或者(==)会给你带来什么? – James

那么我最终发现了什么问题。感谢您的回复,我相信您可能没有遇到缺乏信息的答案。

问题出在我传递给函数的text值上。 text最后包含"",这就是为什么比较不起作用。

+0

那么你如何解决你的问题? – user3236289

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

+0

我改变了没有-1的长度,仍然一样。 – satran

+0

使用长度为1是绝对错误的。子字符串的长度必须与真实的文本长度相同,不能小于1。 – bitfiddler

+0

那么我改变了代码使用切片而不是子字符串。仍然是同样的问题。 – satran