-1

Here is the code that I am executing:

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");
    });
}     

The matchedText == text always returns false.

This is what I get when I play around with the console:

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

I do realize that and === will always check if both the objects are the same and I have read JavaScript equal operations anomalies and Javascript string equality.

Is there something wrong in the code that I am overlooking?

9
  • Check matchedText.length and text.length. Nov 22, 2013 at 5:28
  • What sort of object is issues and what does issues.each() pass to the function (i.e. what is issue)? What does issue.get() return?
    – RobG
    Nov 22, 2013 at 5:31
  • Maybe fence post error? var matchedText = value.substring(0, text.length - 1); did you try without - 1? what does matchedText.valueOf() === text.valueOf() or with (==) give you?
    – James
    Nov 22, 2013 at 5:32
  • Did you checked with trim();
    – Krish R
    Nov 22, 2013 at 5:36
  • @James matchedText.toString() == text.toString() returns false and so does ===
    – satran
    Nov 22, 2013 at 5:44

2 Answers 2

0

I think you are misusing the subString() method. If you use subString(), use the length without -1.

3
  • I changed the length without -1 and still the same.
    – satran
    Nov 22, 2013 at 5:47
  • Using length-1 is definitely wrong. The substring must be the same length as the text for the caparison to be true, not 1 less.
    – bitfiddler
    Nov 22, 2013 at 5:58
  • Well I changed the code to use slice instead of substring. Still the same problem.
    – satran
    Nov 22, 2013 at 6:25
0

Well I eventually found out what was the problem. Thanks for your responses and I believe you might not have come across the answer for the lack of information.

The problem lied in the text value that I was passing to the function. The text contained a "" at the end and that's why comparison just did not work.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.