• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 514
  • Last Modified:

Possible to remove duplicates from a selectlist populated by json?

I have a selectlist that is populated by json. The issue I'm having is I get duplicates in my selectlist and I'm hoping I can remove them as the selectlist loads.

I have a fiddle to show you what I mean:
See Fiddle

The actual vehicles aren't really 'duplicates' but since I'm only showing a select amount of the supplied data (name and basic style) they appear duplicate in the select.

I came across juery.unique() but not sure if that would work... or how to apply it in this instance. Any help would be appreciated.

  • 2
1 Solution
There are not actually any duplicates in your example.  

For example:
<option value="200420764">ATS Luxury 4dr Sedan</option>
<option value="200420763">ATS Luxury 4dr Sedan</option>
<option value="200420762">ATS Luxury 4dr Sedan</option>

While the text is the same, the value is different.

Notice this in your JSON:

                "id": 200420764,
                "name": "Luxury 4dr Sedan (3.6L 6cyl 6A)",

                "id": 200421837,
                "name": "Luxury 4dr Sedan (3.6L 6cyl 6A)",
Because the ID is the key and it is unique, using something like juery.unique() would not help.
You could use contains() like this:
if (!$('#newmodel option:contains("'+ String(data.models[i].name +" " + data.models[i].years[j].styles[k].name).replace(/ \([^)]*\)/, '') +'")').html()) $("#newmodel").append(option);

Open in new window

Here is an example:
tjyoungAuthor Commented:
Thats fabulous. Never would have figured that out. Many thanks!
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now