user1152226
user1152226

Reputation: 323

JavaScript boolean compare always fails

This Boolean comparison always returns false but it is comparing false == false in my getColumnsFunction(). It should return true, and I have no clue why. Here is the code:

function getModelColumns(exlude, modelName){    
    var model = Ext.ModelManager.getModel(modelName).create();
    var fields = model.fields.items;

    for(var x in fields){
        console.log( inArray(exlude, fields[x].name == false),
                    'boolean_compare', 
                     inArray(exlude, fields[x].name));
     }

}
function inArray(arr,val){
    for(var x in arr){
        if(arr[x] === val)
            return true;
    }

    return false;
 }

Here is what is in console.log() for all fields in that for loop:

false "boolean_compare" false 

They are both equal to false, why they heck does that boolean comparison return false?

Edit: forgot a '(' it should be inArray(exlude, fields[x].name ) == false

Upvotes: 0

Views: 2578

Answers (3)

Guffa
Guffa

Reputation: 700680

This expression:

inArray(exlude, fields[x].name == false)

will first compare the name to false, and as they are not equal it will call inArray(exclude, false). Unless it's an array containing the value false it will always return false.

I think that you want:

inArray(exlude, fields[x].name) == false

which will search for the name in the array, then compare the result to false.

Upvotes: 3

shareef
shareef

Reputation: 9581

you can debug with firebug on FF or web tools on Chrome i would do it.
but give us jsfiddle demo on it jsfiddle

but i think may be console.log has something to do with it i got same problem one dont use console.log use alert test it .

Upvotes: 0

nscrob
nscrob

Reputation: 4493

I'm guessing the field name is not explicit defined as boolean, so in extjs by default is string. You are comparing "false" with false .

Upvotes: 1

Related Questions