Remove duplicate empty values from select tag

Ravi

I have below select tag

<select id="subCategorySelected">
    
   <option value="Select Sub Category">Select Sub Category</option>
                    
   <option value=""></option>
                    
   <option value="subcat1">sub cat 1</option>
                    
   <option value="subcat2">sub cat 2</option>

   <option value=""></option>
                    
   <option value="subcat4">sub cat 4</option>
                    
   <option value=""></option>
              
</select>

I need to filter items in the tag and remove all duplicate elements in below way

<select id="subCategorySelected">
    
   <option value="Select Sub Category">Select Sub Category</option>
                    
   <option value=""></option>
                    
   <option value="subcat1">sub cat 1</option>
                    
   <option value="subcat2">sub cat 2</option>
                    
   <option value="subcat4">sub cat 4</option>
              
</select>

I have tried in below ways as suggested in some other question but both are not working

1)
var map = {};
$('#subCategorySelected').each(function () {
    if (map[this.value]) {
        $(this).remove()
    }
    map[this.value] = true;
})


2)
$("#subCategorySelected").val(function(idx, val) {
  $(this).siblings('[value="'+ val +'"]').remove();
});

Thanks in advance

dave

If you know the only duplicates are empty values you could just do:

$("#subCategorySelected option[value='']").slice(1).remove();

otherwise your first method works, just change the selector to:

$("#subCategorySelected option")

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to remove duplicate values from multiple select?

How to remove duplicate values from dynamic select in Javascript?

how to remove empty strings from list, then remove duplicate values from a list

Remove duplicate values from hashtables

Remove duplicate values from api

how to remove duplicate values from a list but want to include all empty strings from that list

Remove duplicate node set based on element tag is empty

How to remove a selected option from another select tag when i have five select with same options and values?

remove empty tag pairs from HTML fragment

Remove Duplicate ids from select query

How to remove duplicate values from the python dataframe?

Remove duplicate values from a multidimensional array in PHP

remove consecutive duplicate with a certain values from dataframe

How to remove duplicate values from an array in PHP

remove duplicate values from json data

How to remove duplicate values from a multidimensional array?

How to remove duplicate values from a HashMap

Remove duplicate values from an array of objects in javascript

Remove duplicate values from JS array

remove duplicate values from a list with multiple properties

How to remove duplicate values from a RDD[PYSPARK]

remove duplicate values generated from mysql

Remove duplicate values from a string in java

Cannot remove duplicate values from a mysql table

Remove duplicate values from NSArray while appending

Coffeescript: Remove duplicate values from json object

Remove duplicate values from a multidimensional array

How to remove duplicate from list of values

Remove duplicate values from view - Angular

TOP Ranking

HotTag

Archive