Avoid repeating the same options in a dropdown menu

Here is the HTML code I am working with:

<div layout="row" layout-align="center center">
            <md-select placeholder="Property type" ng-model="id" md-on-open="loadProperties()" style="min-width: 200px; padding: 20px;">
                <md-option ng-value="id" ng-repeat="id in list" >{{id.propertyType}}</md-option>
            </md-select>
</div>

This code fetches data using Angular from the following JSON:

    {
  "properties": [
    {
      "propertyId": "5702581e3678da025f79c83d",
      "price": 493967,
      "mainImg": "//loremflickr.com/668/501/house,facade",
      "images": [
        {
          "thumbUrl": "//loremflickr.com/372/279/property,interior",
          "mainUrl": "//loremflickr.com/668/501/property,interior"
        },
        ...
      ],
      "beds": 6,
      "propertyType": "Terraced House",
      "propertyAddress": "628 Arlington Avenue, Jardine, California, 2444",
      "description": "Fugiat voluptate sunt deserunt sit tempor culpa.... aute qui. Nulla adipisicing officia sunt incididunt cillum qui exercitation officia labore esse minim. Enim occaecat mollit amet laboris id excepteur elit mollit ex tempor exercitation duis.\r\n"
    }

The JSON contains various properties with unique propertyIDs but sometimes similar PropertyTypes. However, when trying to create a select box like this Select-img, duplicate values are displayed as shown in the image provided.

How can I prevent these duplicated values from appearing?

Thank you for your help and apologies for any language errors.

Answer №1

If you want to prevent duplicates, you can utilize the unique filter. Here is an example of how you can implement it:

<div layout="row" layout-align="center center">
   <md-select placeholder="Property type" ng-model="id" md-on-open="loadProperties()" style="min-width: 200px; padding: 20px;">
      <md-option ng-value="id" ng-repeat="id in list | unique:'propertyType'" >{{id.propertyType}}</md-option>
   </md-select>
</div>

It's important to note that AngularJS does not come with a built-in unique filter, so you will need to create your own. Here is an example of how you could do this:

app.filter('unique', function() {
   return function(input, key) {
      var unique = {};
      var uniqueList = [];
      for(var i = 0; i < input.length; i++){
          if(typeof unique[input[i][key]] == "undefined"){
            unique[input[i][key]] = "";
            uniqueList.push(input[i]);
          }
      }
      return uniqueList;
   };
});

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

I encountered a "TypeError: keys must be a string" message while using json.dump()

When trying to convert a dictionary with a tuple key into JSON using this simple code: In [11]: import simplejson as json In [12]: data = {('category1', 'category2'): 4} In [13]: json.dumps(data) An error is encountered: TypeError ...

Access the angular controller using the radio button option

I am looking to showcase data in an HTML table retrieved from an Angular controller. I have the controller set up but I am unsure about how to display the data when a radio button is selected. <div class="radio"> <label class="radio-inline" ...

Using PHP to store data in JSON format for GoJS

How can I structure my json data to fit the requirements of a js library like GoJs? The expected format for GoJs is as follows: model.nodeDataArray = [ { key: "1", username: "Don Meow", source: "cat1.png" ...

Ways to verify the functionality of a webpage once it has been loaded on my local machine's browser

I manage a website that is currently hosted on a server and being used by thousands of visitors. The site is created using Java and soy template technology. I am looking to test the frontend rendering and JavaScript files. Can this be done directly from my ...

Using animate.css and jQuery for sequential animations

Is there a way to make them fadeInUp one after the other? Check out the demo http://jsfiddle.net/uz2rm8jy/4/ Here is the HTML structure: <div class="c one"></div> <div class="c two"></div> <div class="c three"></div> ...

The app's functionality is disrupted by a malfunctioning Appframework jQuery

Having some trouble implementing the jQuery UI autocomplete widget in a PhoneGap app using the jq.appframework.js plugin. Here is how I have included the necessary scripts and styles: <script src="appframework/jquery.js"></script> <script s ...

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({}); } }); ...

ThreeJS | The object's position cannot be cloned as it seems to be undefined, however, this is unexpected

My code problem is not as complex as it seems. The issue lies within the section marked by the "ERROR APPEARS HERE" comments. The error message I am encountering reads: Uncaught TypeError: Cannot read property 'position' of undefined This er ...

Changing HTML lists into JSON format

I am facing a slight issue with my HTML template while trying to convert it to JSON. My goal is to convert a list of HTML messages into JSON format. I appreciate your assistance in advance. HTML Template <div class="message"> <div class="mes ...

Can you provide the keycodes for the numpad keys: "/" and "." specifically for the libraries I am utilizing or any other library that does not overlook them?

I've hit a roadblock with my Chrome Extension development. Despite using two different methods to add keyboard functionality, the keys "/" for divide and "." for decimal on the keypad are just not registering. I've attempted to tackle this issue ...

Change the values of every element in an array

Looking to update the values of every item in an array list? if (country.hostel) { country.hostel.forEach(function (hostel, index) { hostel.room.forEach(function (room, index) { room = {code:{value: BOOKED}}; ...

Performing an AJAX request to send data containing special characters

What is the best way to send a large string with special characters like '%' and '&' to my PHP page using AJAX POST? In simple terms, how can I encode these characters in JavaScript and then decode them in PHP? ...

What is the best way to employ document.addEventListener in TypeScript?

I am currently learning Ionic v2 and I am using document.addEventListener, but I am encountering errors as shown below: > [11:10:21] ionic-app-scripts 0.0.47 [11:10:21] build dev started ... [11:10:21] clean started ... [11:10:21] clean finished in ...

I am attempting to create a skybox, yet it seems like I am overlooking a crucial element

Currently, I am attempting to create a skybox but have encountered difficulties with various tutorials. Initially, I tried to use an array approach to pass parameters for the material based on a previous example, but it seems that the method has been updat ...

CombineReducers takes all the reducer content and unpacks it into a single reducer

As I work on developing a small application, I am contemplating the best strategy for organizing reducers within it. My objective is to retrieve JSON data from the application state and structure it as shown below: { "fruits": [ {"appl ...

Guide on loading a JSON file in Play framework utilizing Scala

In my project, I am looking to retrieve a list of cities from a JSON file within one of my controllers. The intention is to then pass this information to a view for display. The location where I have stored the file is: app/assets/jsons/countriesToCities. ...

Having trouble with Vuejs Ternary Operator or Conditional in v-bind-style?

Having some unexpected issues while trying to implement a style conditional for items in Vuejs. I have come across Stack Overflow posts on how to use a ternary, either through an interpolated string or a computed style object. I've attempted both met ...

Tips for accessing a RESTful web service using an AJAX call

I am having trouble calling my REST web service using AJAX. The URL for my service is ''. I can successfully call this service from a REST client in the Firefox browser, but when I try to call it from AJAX, I get an error. Here is my AJAX call : ...

Mastering server requests in Angular 5

I have come across recommendations stating that server requests should be made via services and not components in order to ensure reusability of functions by other components. Ultimately, the server response is needed in the component. My query pertains t ...

The indicated processing instruction is incompatible with the provided payment source. PayPal's hosted fields for credit card payments do not support this specific processor

I'm currently working on integrating credit card payments with hosted fields into my checkout process. However, I keep encountering an UNPROCESSABLE_ENTITY error when making the confirm-payment-source request through the PayPal JS SDK. Here is the co ...