2

This is the dropdown list box. I want to remove the duplicate "Apple" using javascript.

<select id="Fruits">
<option value="Apple" >Apple</option>
<option value="Orange">Orange</option>
<option value="Mango">Mango</option>
<option value="Apple" >Apple</option>
</select>
9
  • 3
    [].slice.call(Fruits.options).map(function(a){if(this[a.value]){ Fruits.removeChild(a); }else{this[a.value]=1;} },{}) Commented Jul 17, 2013 at 19:00
  • @dandavis ... why didn't you just put that as an answer..? Commented Jul 17, 2013 at 19:03
  • @Shawn31313 the downvote scared me away... Commented Jul 17, 2013 at 19:05
  • @dandavis is that plain JavaScript or using a JavaScript library? Commented Jul 17, 2013 at 19:41
  • 1
    @dandavis post it as an answer. Commented Jul 17, 2013 at 20:09

2 Answers 2

3

This is @DanDavis answer for reference, I'm adding it as, you know, public service. Plus a bit of formatting and a couple of alternatives to cover similar use cases.

var fruits = document.getElementById("Fruits");

[].slice.call(fruits.options)
  .map(function(a){
    if(this[a.value]){ 
      fruits.removeChild(a); 
    } else { 
      this[a.value]=1; 
    } 
  },{});

If you are working with a select populated by a database (a pretty obvious use case) and the values are ids and the innerText is the duplicates you want to remove. Then instead you'd want:

[].slice.call(fruits.options)
  .map(function(a){
    if(this[a.innerText]){ 
      fruits.removeChild(a); 
    } else { 
      this[a.innerText]=1; 
    } 
  },{}); 

Furthermore, if you want to retain the selected option (it might not be the first occurrence matched.) Try this:

[].slice.call(fruits.options)
  .map(function(a){
    if(this[a.innerText]){ 
      if(!a.selected) fruits.removeChild(a); 
    } else { 
      this[a.innerText]=1; 
    } 
  },{}); 
Sign up to request clarification or add additional context in comments.

3 Comments

I know this is old however, I wanted to know if there was a way to map ID's and check for each of them?
@Especially if you post this as a question, I will answer it. (You may find a similar question already exists.) Seems like you just need an array of the ID's as strings and the map those, and return the element found, or null. You could optionally compact the array (with lo-dash/underscore etc.) - if you post a question, link it here.
I've posted the question here: stackoverflow.com/questions/25562969/… I tried mocking up something but I'm terrible at javascript. Thank you!
1

In ES3 POJS you could do this.

Javascript

function removeDuplicateOptions(selectNode) {
    if (typeof selectNode === "string") {
        selectNode = document.getElementById(selectNode);
    }

    var seen = {},
        options = [].slice.call(selectNode.options),
        length = options.length,
        previous,
        option,
        value,
        text,
        i;

    for (i = 0; i < length; i += 1) {
        option = options[i];
        value = option.value,
        text = option.firstChild.nodeValue;
        previous = seen[value];
        if (typeof previous === "string" && text === previous) {
            selectNode.removeChild(option);
        } else {
            seen[value] = text;
        }
    }
}

removeDuplicateOptions("Fruits");

On jsfiddle

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.