-2

I want to add element to my drop-down list wihtout duplicates

for (var i = 0; i < queues.entities.length; i++) {
      var queuesName = queues.entities[i].name;

 $("#brandFilter").append(
        $(new Option(queues.entities[i].name.substring(0, 2)))
      );
     
    }

Here is my list

 <div>
                  <b>Brand</b>
                  <select
                    class="form-control input-sm form-control-sm border-0 brandFilter"
                    id="brandFilter"
                    multiple
                    size="3"
                  >
                    <option value="-" selected>--- All ---</option>
               </select>
                 <hr />
                </div>

Thank You So much

  • Are you running this loop multiple times on the page? You'll need to clear the list every time you do that: `$("#brandFilter").html("");` – psiodrake Mar 01 '22 at 09:10
  • Is the issue (as above) that you're running the same code and it's adding elements again? Or does your `queues` have duplicates? – freedomn-m Mar 01 '22 at 09:23
  • it has duplacate becasue i am basing on other queues. and i am taking out 2 first letters. 2 first is equal to Country Code. If e have 6 queues with code US they are going to duplicate – Jan Walczak Mar 01 '22 at 09:29
  • i wanted to do something llike if contains (varable) end loop but It didn't work. Maybe i did something wrong – Jan Walczak Mar 01 '22 at 09:30
  • In which case, it's easier to remove the duplicates first, see [this question](https://stackoverflow.com/questions/2218999/how-to-remove-all-duplicates-from-an-array-of-objects) for various solutions. – freedomn-m Mar 01 '22 at 09:47

1 Answers1

0

An easy solution would be to remove the duplicates entities first.

  const uniqueQueuesEntities = [...new Set(queues.entities)];

  for (var i = 0; i < uniqueQueuesEntities.length; i++) {
    var queuesName = uniqueQueuesEntities[i].name;

    $("#brandFilter").append(
      $(new Option(uniqueQueuesEntities[i].name.substring(0, 2)))
    );
  }
Simon Leroux
  • 447
  • 2
  • 5