Quick shorthand if statement that will constantly return true

I am dealing with a dropdown menu that displays different groups. Each group has a property (boolean) indicating whether the owner is a user or another group.

If the group's owner is a user, I obtain the ownerID of the group and compare it with an array of users to find the matching user and set that user as the selectedOwner. On the other hand, if the group's owner is another group, I iterate through all my groups to identify the match and set that group as the selectedOwner.

This is the function in my controller:

$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;
        };
    };      
};

Every time the dropdown changes, it triggers the groupOwner function which examines the selectedUser.ownerIsUser property to determine which array to search into, users or groups.

However, the temp variable always returns true, regardless of what the selectedGroup owner property is set to.

This is how the objects are structured:

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

Answer №1

Your variable is set to the string "False" and not the boolean value false. Since "False" is a string, it always evaluates as true.

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

This code will always assign temp = $scope.users

Here is an example of testing in the JavaScript console :

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

Answer №2

the particular item is not defined correctly

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

It must be defined with complete variable values

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

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

"Deleting a specific element from an array schema in a Node.js application

I need to update my database by setting ProjectSubmit.pending = true in order to remove accepted proposals. However, I also added these proposals to an array schema when they were accepted, and now I need to remove that index from the array. Here is my sc ...

Order child property in AngularJSWe are sorting properties in

Greetings! I am in search of a way to arrange an angular list according to a specific child property. Here is the model that I currently have: $scope.data = [{name:"John",type:{talent:"genius",val:99}}, {name:"Paul",type:{talent:"genius", ...

Retrieve the element's value in relation to a different parent element

[JavaScript] How can I access the value of a textbox related to a button through jQuery? The event is triggered when the .button-action is clicked <td class="dmsInput"> <input type="text" maxlength="4" size="4" class="d"> </td> <td& ...

Modifying the State array is beyond my control

I'm facing an issue passing the codes correctly here, so I'll explain the problem using images. (REMEMBER: All these are within the same component) Issue: I have a single state value: state = { base: [ {tomato: false}, {egg: true} ], ...

Using the Coinbase.com API with HMAC authentication in a Node.js environment

I recently delved into some node.js documentation, specifically crypto and https, in an attempt to utilize the coinbase.com API within my coin.js file to retrieve the bitcoin balance of my account. However, I am encountering errors. Upon running the code w ...

How can multiple elements be connected to an onclick action?

I'm having trouble figuring out how to use an onClick action with jQuery for all the elements in a list of attached files. Here is a snippet of my HTML file: <p>Attachments</p> <div> <ul id="attach"> <li id="KjG34 ...

The functionality of an external Javascript library fails to operate properly once the website is deployed to a hosting

Encountering a peculiar bug with my website at . The website is supposed to load both the JQuery and EasyUI libraries. JQuery is loading correctly as a function to print out "ready" when the page loads. The issue arises when trying to drag products onto th ...

Watching a VideoJS video does not pause even after closing a Bootstrap modal

I am struggling with a modal on my website that contains a video. Even when I close the modal by clicking outside of it or using the close button, the video continues to play in the background. I have tried all the solutions I could find on stackoverflow ...

Exploring the intricacies of Node-Craigslist syntax

Greetings! I am currently utilizing the node-craigslist package found at https://github.com/brozeph/node-craigslist and I am in need of assistance with some syntax related to the details method. The documentation provides the following example: client . ...

Exploring the power of Angular by implementing nested ng-repeat functionalities:

I am currently working on an ng-repeat feature to add items from the array (album array). Everything seems to be functioning correctly. However, I also have a colors array that should assign different background-colors to the card elements of the album arr ...

Error message: "IAngularStatic type does not have property IScope" caused by Typescript Directives

I'm working on creating an Angular Directive using TypeScript to share a scope item. I created an interface that inherits from ng.IScope, but Visual Studio Code is showing me a warning: "Property IScope does not exist on type IAngularStatic". I am usi ...

fuzzy edges on an HTML5 canvas

When attempting to draw vertical lines on a canvas with a 1px width, some lines may appear wider or blurry. Various solutions, such as subtracting 0.5px or translating coordinates, have been suggested but do not seem to work in this case. Attempts have be ...

The requested URL socket.io/1/?t= cannot be located

I've created a web application running on rails 4 at localhost:3000. A client-side angularjs is also incorporated into the project. The socket.io.js file has been placed in the public folder of my rails app. In my angularjs client code, I have the fol ...

Strange behavior noticed in the app.get() method of Node.js Express

Seeking clarification regarding the behavior of app.get() in Express. It appears that the function is not triggered when the path includes .html at the end. In the code snippet provided, the console logs "test" if attempting to access /random/example, bu ...

Utilizing Jade to access and iterate through arrays nested in JSON data

Take a look at this JSON data: { "Meals": { "title": "All the meals", "lunch": ["Turkey Sandwich", "Chicken Quesadilla", "Hamburger and Fries"] } } I want to pass the array from this JSON into a jade view so that I can iterate over each item in ...

Eliminate forward slashes from the telephone number

I am trying to figure out how to format a USA phone number by removing slashes and brackets. I have searched on Google, but I cannot find the answer. I would like to use regex to achieve this, while keeping the plus sign at the start intact. For example, ...

Issue encountered while trying to implement a recursive function for mapping through nested elements was not producing the

I am currently working on recursively mapping through an array of nested objects, where each object can potentially contain the same type of objects nested within them. For example: type TOption = { id: string; name: string; options?: TOption; } con ...

Default value of custom Component v-model should be set for v-text-field

Help! I need to set a default value for a v-text-field in a custom component, but all my attempts to override the editedItem.color v-model have failed. I work with Laravel PHP and could really use some assistance from my fellow developers here. I'm n ...

React - The prop value remains static even after the parent component updates its related state

My Application consists of two main components: Button and Input https://i.sstatic.net/9cfCu.png Upon clicking the Click to update state button, the input value initially set as May should be updated to May New. However, I noticed that this change is not ...

Navigating with React Router v6 beyond the confines of individual components

When using react-router v5, I would create the history object like this: import { createBrowserHistory } from "history"; export const history = createBrowserHistory(); Then I would pass it to the Router like so: import { Router, Switch, Route, Link } from ...