Using AngularJS to iterate through a list of objects and apply filters based on the

Is it possible to iterate over objects and check their properties?

{
 Obj1 : { visible : true, value : 2}
 Obj2 : { visible : false, value : 4}
 Obj3 : { visible : true, value : 6}
}

HTML:

<ul ng-show="data.templates.attachments">
        <li ng-repeat="(key, value) in data.templates.attachments | filter :       value.visible">{{key}}</li>
</ul>

If I only want to display the keys that are visible, this code works perfectly. However, I also want to hide the ones that are invisible.

Answer №1

Check out this solution

<li ng-repeat="(key, value) in data | filter: {visible: true}">{{value.value}}</li>

Demo here

Update:

Implement a filter by individual object property

// specific to one property, for instance {a: 1}
myApp.filter('filterByProperty', function () {
  return function(items, field) {
      var result = {},
          key    = Object.keys(field).pop(),
          value  = field[key];

      angular.forEach(items, function(el, index) {
          if (el[key] === value) {
              result[index] = el;
          }
      });

      return result;
  };
});

See an example here

Answer №2

Even though Alexander has already provided an answer to the question, there is still a way to retain the element for each object in the DOM without displaying it (as the visible property might give the impression that it will simply not be shown, rather than being omitted from the DOM).

To achieve this, you can utilize the ng-class directive. http://jsfiddle.net/naeemshaikh27/dKjz5/48/

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

What is the CoffeeScript alternative for () => { return test() }?

Currently, I am attempting to write this code in CoffeeScript and finding myself at a standstill... this.helpers({ events: () => { return Events.find({}); } }); ...

When the div is refreshed, the input box should retain the text instead of clearing it

I am facing an issue with a div that refreshes every 3 seconds. There is an input box inside the div, but whatever I type gets cleared out in 3 seconds. Is there a way to prevent the text from being cleared out and keep it inside the input box? index.js ...

Obtaining verified user information through Express using a JWT token

Having recently ventured into Express, I have prior experience with Laravel and PHP. Currently, my concern lies with a database schema structured like this: Users table : created_by updated_by In Laravel, I could easily auto-fill the 'created_by&ap ...

Utilizing React JS to dynamically populate a table with data from an external JSON file

As a newcomer to the realm of React, I am facing challenges in displaying my JSON data in a table. class GetUnassignedUsers extends React.Component { constructor () { super(); this.state = { data:[] }; } com ...

Angular directive automatically refreshes when the scope is updated

One issue I'm facing is that many of my directives, which will soon become components, rely on variables set by other directives. Currently, I find myself having to watch the scope in each directive to detect any changes, making the code unnecessarily ...

The resizing function on the droppable element is malfunctioning on Mozilla browsers

I've been working on making one div both droppable and resizable. Surprisingly, everything is functioning perfectly in Chrome but not in Firefox. If you'd like to see the issue for yourself, here is my jsFiddle demo that you can open in Firefox: ...

Passing an object in an ajax call to a function: a comprehensive guide

@model IEnumerable<HitecPoint.BlackBox.Models.SMSReportModal> <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.0/jquery.min.js"> </script> <script type="text/javascript"> var MyAppUrlSettin ...

Set up the configuration for express to access an external API through proxy.conf.json while in production mode

I am currently managing two applications on Heroku, one being myserverapi (built with Spring Boot) and the other being a client-side Angular app named client. The server is hosted at myserver.heroku.com while the client resides at myclient.heroku.com. At t ...

The Bootstrap Navbar is causing the navigation bar to span across two lines

I'm having an issue with my navbar taking up two lines on desktop resolution, but fitting perfectly on mobile. I've spent hours going through the code with no success. Any help would be greatly appreciated. The HTML code is provided below. <! ...

Enhancing SOAP message attributes with node.js (node-soap)

Spent the entire day struggling with this issue, and I would greatly appreciate any assistance! I am currently developing an application that interacts with a 3rd party SOAP web service. The application is built using node.js and utilizes node-soap. Howev ...

Troubleshooting: Issues with accessing object properties in a function in AngularJS

In my controller, I have a function that checks the day and changes the isOpen property of an object based on the time. The object is retrieved using the code snippet below: $http.get('js/data.json').success(function(data) { $scope.locations = ...

Is it possible to use file upload for sending via Ajax's POST method?

Let's talk about the scenario at hand Here's what happens in a single form: 1) The user clicks on the 'browse' button, which opens a dialog to select an image file for uploading. Example: input id='img_upload' name="ufile" ...

What is the process for removing a mesh that has been uploaded using a function and then added to an Object3D?

// Defining variables for 3 obj models: cushion, backrest, and frame, and a group variable chair01 to include all 3 obj models var cushion; var backrest; var frame; var chair01 = new THREE.Object3D(); ...

Testing the functionality of an Express Rest API with Mocha unit tests

I have just started diving into the world of unit testing. While I've been successful in running simple tests such as "adding two numbers and checking if the result is greater than 0", my goal now is to develop a REST API using Test-Driven Development ...

Appending a property to a JSON object using JavaScript

What is the method for adding a new field to a JSON object in JavaScript? I have seen examples using arrays, but I specifically need it with a JSON object. Essentially, how can I transform this: { "hello":"this is cool" } into this ...

Issues have been raised with IE11's refusal to accept string(variable) as a parameter for the localStorage

Why is it that Internet Explorer does not recognize a string variable as a parameter for the setItem method, even though it works fine in Chrome? For example, in IE: This code snippet works: var itemName = 'anyname'; localStorage.setItem(itemN ...

What is the best way to show nested objects in JavaScript?

let paragraph = document.createElement('p'); document.body.appendChild(paragraph) const fruit = { type: 'Apple', properties: { color: 'Green', price: { bulk: '$3/kg', smallQty: '$4/kg' ...

What could be the reason behind the disappearance of text from the previously highlighted button in my calculator's "button grid" when I change the highlighted button?

Currently, I am in the midst of creating a tip calculator with a grid consisting of various percentage buttons. My main objective is to change the font and background color when any tip button is selected. Nevertheless, an issue has surfaced - whenever I h ...

Utilizing a web application on dual monitors

I am currently exploring options for creating a browser web app that can run on dual monitors. I have a secondary monitor where I would like to display a separate window in fullscreen mode without the need to manually drag and resize it, while keeping my m ...

Determine whether Three.js is compatible

Looking for a way to determine if the browser can handle three.js? I came across using detector.js provided by three.js with this line of code to check if WebGL or canvas is supported: renderer = Detector.webgl ? new THREE.WebGLRenderer() : new THREE.Canv ...