'Jquery adds items to the drop-down list without duplicates
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
Solution 1:[1]
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)))
);
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Simon Leroux |
