-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?

Community
  • 1
  • 1
satran
  • 1,222
  • 4
  • 16
  • 27
  • Check `matchedText.length` and `text.length`. – Felix Kling Nov 22 '13 at 05: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 '13 at 05: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 '13 at 05:32
  • Did you checked with trim(); – Krish R Nov 22 '13 at 05:36
  • @James `matchedText.toString() == text.toString()` returns `false` and so does `===` – satran Nov 22 '13 at 05:44
  • @FelixKling aaah... the length is different. I changed `text.length - 1` to `text.length` and still the same. – satran Nov 22 '13 at 05:47
  • @RobG issues is a backbone model. – satran Nov 22 '13 at 05:48
  • @Satyajit hmm just to be sure you could try `+ 1` (to rule out fence-post-error lol) but perhaps it's some crazy javascript quirk? What does `issue.trigger("hide")` do? – James Nov 22 '13 at 05:58
  • @James it triggers an event which will be caught by the backbone model. I have commented out the code and yet it has the same problem. It is just bonkers. – satran Nov 22 '13 at 06:25

2 Answers2

0

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

bitfiddler
  • 2,095
  • 1
  • 12
  • 11
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.

satran
  • 1,222
  • 4
  • 16
  • 27