-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. Commented 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? Commented 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? Commented Nov 22, 2013 at 5:32
  • Did you checked with trim(); Commented Nov 22, 2013 at 5:36
  • @James matchedText.toString() == text.toString() returns false and so does === Commented 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.

Sign up to request clarification or add additional context in comments.

3 Comments

I changed the length without -1 and still the same.
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.
Well I changed the code to use slice instead of substring. Still the same problem.
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.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.