How to extract only the truthy keys from an object using Angular.js

I am looking to retrieve only the keys with a true value in the data object and display them in the console with the specified format:

Object {
   Agent=true,
   Analytics / Business Intelligence=true,
   Architecture / Interior Design=false
}

The categories are listed in ng-model checkbox inputs on the front end like this:

<li ng-repeat="ai in industry | filter:searchByIndustry" class="checkbox"> 
    <label>
    <input type="checkbox" class="css-checkbox" id="checkboxCategories{{ai.id}}" ng-model="dataCategory[ai.name]" />
    <label class="css-label chrome-style" for="checkboxCategories{{ai.id}}">{{ai.name}}</label>                 
    </label>
</li> 

When a user checks the checkbox, the category name is assigned a value of true. On unchecking, it becomes false. I want to store the true values in a string in my Angular controller.

$scope.$watch(function () {
        return {
            useCat  :$scope.dataCategory
        }
    }, function (value) {

var searchMeta = [];
for( var l in value.useCat ) {
   if (l==true) {
      //code
      searchMeta.push(l)
   }
}
console.log($scope.searchMeta);
}, true );

Answer №1

Finally got it to work after a few tries:

let searchMeta = [];
for (let item in value.useCat) {
    if (value.useShirts[item]) {
       console.log(item);
       searchMeta.push(item);
    }
    console.log(value.useCat[item]);
}
console.log(searchMeta); 

Appreciate the help!

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

Is there a method for verifying the application signature in Ionic?

For the past 2 days, I've been on a quest to find information about app certificate validation libraries/functions in Ionic. After discovering SignatureCheck.java for Android (link: enter link description here), I wonder if there is a similar solution ...

The concept of dynamic schema in Mongoose allows for flexibility and

Currently, I am working on creating a product schema that involves setting pricing in different currencies. However, I am facing confusion regarding how to dynamically create currency options. The pricing may vary in one currency or multiple currencies as ...

"npm is the go-to tool for managing client-side JavaScript code

As I delved into a document outlining the npm Developer Guide, a question sprang to mind. Is it within the realm of possibility to construct a web client application using javascript/css/html with npm at the helm? And if so, might there be examples avail ...

Placing content retrieved from a MySQL database within a form displayed in a ColorBox

Having difficulty inserting text from a MySQL database into a form (textfield) inside a ColorBox. The current script is as follows: <a href="#" class="bttn sgreen quote1">Quote</a> var postQuote[<?php echo 4id; ?>]=<?php echo $f ...

Can the image be adjusted based on different time zones around the world?

How can I create an HTML banner that changes images based on the time of day? I want one image to display between 7pm and 6am, and another image during the rest of the day. I came across a helpful website that achieves this by changing the image according ...

tsc and ts-node are disregarding the noImplicitAny setting

In my NodeJS project, I have @types/node, ts-node, and typescript installed as dev dependencies. In the tsconfig.json file, "noImplicitAny": true is set. There are three scripts in the package.json file: "start": "npm run build &am ...

Incorporating an additional row into a material table

Currently, I have implemented a mat-table with various features including drag and drop functionality and dynamic columns. However, I am facing an issue while trying to add row filters under the table header. I attempted to simply insert a <mat-row> ...

Unable to remove the most recently added Object at the beginning

I am currently working on a project to create a dynamic to-do list. Each task is represented as an object that generates the necessary HTML code and adds itself to a div container. The variable listItemCode holds all the required HTML code for each list it ...

Synchronizing live data from the database to the front end of a Java-powered web application

I'm currently developing a customer support web application using Java, where I need to display the status of "Customer Representatives (CR)" in the front-end - whether they are available, busy, or away on the phone. When a CR ends a call with a cust ...

Tips for validating user input in AngularJS without using a form tag

Within a popup, I am displaying HTML content that has been copied from another div and shown in the popup. I need to validate this input field to ensure it is required, and display an error message below the input box. <!-- HTML code for changing zip c ...

Instructions for passing a JavaScript variable to a PHP MySQL database

I am attempting to integrate a JavaScript variable into PHP MySQL, but I'm encountering issues with the insertion. Currently, it is being inserted as <javascript>document.write(window.outerWidth); </javascript> x <javascript>document ...

Leveraging a JavaScript variable within a PHP snippet

Similar Question: Sending a PHP string to a JavaScript variable with escaped newlines Retrieving a JavaScript variable from PHP I am trying to work with a Javascript function that accepts one variable, having some PHP code embedded within it. I am ...

eliminating the hues beneath the lines on Morris region charts

I'm seeking advice on how to remove colors below the lines in Morris area charts. Any ideas? Here's the code snippet I've been using: Morris.Area({ element: 'area-example', data: [ { y: '2006', a: 100, b: 90 }, ...

Creating nested subitems in AngularJS ng-optionsWant to create a dropdown list in Angular

How can I populate an Angular ng-options with values from the JSON returned logins data? The problem lies in accessing attributes like login, id, url, etc., which are nested subitems. The HTML <html ng-app="myapp"> <head> ... </head> &l ...

Incorrect handling of Unix timestamps in AngularJS

I'm facing an issue with timestamps. Let me explain - I have a timestamp coded as 1378028575, which corresponds to Sun, 01 Sep 2013 09:42:55 GMT according to this website. The problem arises when I attempt to format this timestamp using Angular's ...

Transforming the JSON data into a text format

I have a JSON object structured like this: { "name": "ok", "country": "US", "phone": "900", "email": "<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="1f70745f727e767331707c">[email protected]</a>", ...

Is the JavaScript Date object consistently displayed in the America/New_York timezone?

The server sends me a time-stamp in milliseconds (Unix time / time from Epoch) with the constant timezone "America/New_York". On my client side, I want to ensure that the time is displayed according to the America/New_York timezone. I have been using Joda- ...

Creating a dynamic select functionality in Drupal forms

Being more focused on backend development, I am facing a challenge that may be simple for jQuery experts. In Drupal, I have two arrays - one containing names of views and the other having displays for each view. To populate these arrays, here is the code s ...

unable to perceive the ng-model

Trying to integrate angular js with a Django server Below is the index.html code snippet: {% load staticfiles %} <html ng-app="blog"> <head> <script type="text/javascript" src="{% static 'js/libs/angular.min. ...

What steps can be taken to prolong the duration of a service?

As a newcomer to angularjs, I am struggling to find documentation or examples on how to extend a basic service in order to utilize its methods from other services. For example, consider the following basic service: angular.module('myServices', [ ...