jquery remove duplicate elements

2020. 8. 11. 17:50JavaScript/JQuery

728x90

https://stackoverflow.com/questions/48579723/jquery-to-remove-duplicate-elements-from-list-by-value

 

jQuery to remove duplicate elements from list by value

I am trying to remove li elements which have duplicate values. I have a list as shown below:

  • Moon
  •  
  • stackoverflow.com

 

 

$(document).ready(function(){
  var found = {};
  $('li').each(function(){
    var $this = $(this);
    if(found[$this.attr('value')]){
      $this.remove();
    }else{
      found[$this.attr('value')] = true;
    }
  });
});
728x90
반응형