Short hand if statement always returning true

I have a drop down menu which lists out groups. Each group contains a property (boolean) that determines if the groups owner is a user or another group

If the groups owner is a user, I get the groups ownerID and compare that with an array of users to determine which user matches that ID and set that user as the selectedOwner. However, if the groups owner is a group, I'll try to loop through all my groups to find the match and set that group as selectedOwner

This is my controller function:

$scope.groupOwner = function (){
    var temp = $scope.selectedGroup.ownerIsUser ? $scope.users : $scope.groups;
    var index = temp.length; 
    console.dir(temp);
    while(index--){
        if($scope.selectedGroup.owner === temp[index].id){
            $scope.selectedOwner = temp[index];
            console.log($scope.selectedOwner);
            break;
        };
    };      
};

Whenever the dropdown is changed it called groupOwner which checked the selectedUser.ownerIsUser property to determine which array I should be looking into, users or groups.

However, the temp variable is always returning true, no matter what the selectedGroup owner property is set to.

This is what the objects look like:

User = { 
name: Demo Administrator,
id: 90,
domain: i:0#.w|itun\demoadmin_compulite,
email: ,
isAdmin: False
 }

selectedGroup =  { 
name: Test Group,
id: 10,
description: ,
owner: 88,
ownerIsUser: False
 }

HTML:

<div class="topRow">
    <label for="entityDropDown">Select a Group:</label>
    <select id="entityDropDown" ng-model="selectedGroup" ng-options="group as group.name for group in groups" ng-change="getGroupInfo(selectedGroup)"></select>
    <div class="delGroupBtn"><a>&#10006;</a>

    </div>
</div>

Console output of object:

Object {name: "Test Group 4", id: "117", description: "", owner: "71", ownerIsUser: "False"…}
description: ""
id: "117"
name: "Test Group 4"
owner: "71"
ownerIsUser: "False"
__proto__: Object

Solved:

$scope.groupOwner = function (){
    //object stores string not booleans
    var isUser = $scope.selectedGroup.ownerIsUser === "True"? true : false;  
    var owner = isUser ? $scope.user : $scope.group;
    var index = owner.length; 
    console.dir(owner);
    while(index--){
        if($scope.selectedGroup.owner === owner[index].id){
            $scope.selectedOwner = owner[index];
            console.log($scope.selectedOwner);
            break;
        };
    };      
};

Answers


Your variable is "False" and not boolean false."False" is a string, which always evaluates as true.

var temp = $scope.selectedGroup.ownerIsUser ? $scope.users : $scope.groups;

Will always set temp = $scope.users

Some javascript console testing example :

> a = "False"
> "False"
> b = false
> false
> testValue = a ? 1 : 2;
> 1
> testValue = b ? 1 : 2;
2

this object is not defined currectly

selectedGroup:  { 
name: Test Group,
id: 10,
description: ,
owner: 88,
ownerIsUser: False
}

It should be defined with all variable values

selectedGroup:  { 
name: Test_Group, // or 'Test Group'
id: 10,
description: '',
owner: 88,
ownerIsUser: false
}

Need Your Help

jquery mobile data-native-menu=false not working on iOS,

ios jquery-mobile multi-select

I am try to use custom multiselect on iOS, using jquery mobile 1.3.2, but when I set the flag data-native-menu=false, nothing happens, no native select is used, and certainly no custom select pops ...