jquery remove duplicate li

后端 未结 5 1785
青春惊慌失措
青春惊慌失措 2020-12-05 16:06
  • microsoft
  • microsoft
  • apple
  • apple
5条回答
  •  悲哀的现实
    2020-12-05 16:27

    uniqueLi = {};
    
    $("#myid li").each(function () {
      var thisVal = $(this).text();
    
      if ( !(thisVal in uniqueLi) ) {
        uniqueLi[thisVal] = "";
      } else {
        $(this).remove();
      }
    })
    

    This build an index (an object) of unique values. For your example, uniqueLi will look like this afterwards:

    {
      "microsoft": "", 
      "apple": ""
    }
    

    So whenever a value is encountered that has been added to the index before, the associated

  • gets removed.

提交回复
热议问题