selected html filter single fields

1

I have the following selected, how do I filter the unique fields for example it is repeating the same option <option value='967'>GOIANIA - GO</option> several times I want it to show only the repeated data once, and it is possible to do this with jquery.

"<option selected="selected" value="0">Cidade</option><option value='967'>GOIANIA - GO</option><option value='967'>GOIANIA - GO</option><option value='967'>GOIANIA - GO</option><option value='967'>GOIANIA - GO</option>"
    
asked by anonymous 11.08.2016 / 13:58

1 answer

1

Understand that the script below only treats the symptom, the problem will still continue, ideally you should perform the filter in your query that populates the select.

var values = [];
var select = document.querySelector("select");
[].forEach.call(select.options, function (option, indice) {
  if (values.indexOf(option.value) > -1) {
    select.removeChild(option);
  } else {
    values.push(option.value);
  }  
});
<select>
  <option value="">Selecione...</option>
  <option value="1">Opção 1</option>
  <option value="2">Opção 2</option>
  <option value="3">Opção 3</option>
  <option value="4">Opção 4</option>
  <option value="2">Opção 2</option>
  <option value="3">Opção 3</option>
  <option value="4">Opção 4</option>
  <option value="5">Opção 5</option>
</select>

EDIT - Tip

Reading your comment, I believe Distinct will work if you decrease the fields of your select .

egm.LstCid.Select(cid => new { Id = cid.Id, Nome = cid.Nome }).Distinct().ToList();
    
11.08.2016 / 14:07