jQuery 2.1 |从列表的顶部到底部删除重复的 li

jQuery 2.1 | Remove duplicate li from top to bottom of list

此代码在无序列表中从下到上删除重复的 li。怎么修改从上到下去掉重复的li?

function removeDuplicates(){
  var map = {};
  $("UL").find(".data").each(function() {
    var value = $(this);
    if (map[value.text()] == null){
      map[value.text()] = true;
    } else {
      $(this).parent('li').remove();
    }
  });
}

如果以上示例无法完成任何其他类型的有效代码示例,我们将不胜感激。

你可以尝试先存储当前元素,然后移除之前的元素,如

function removeDuplicates() {
    var map = {};
    $("UL").find(".data").each(function () {
        var value = $(this);
        if (map[value.text()]) {
            map[value.text()].parent('li').remove();
        }
        map[value.text()] = value;
    });
}

演示:

function removeDuplicates() {
  var map = {};
  $("UL").find(".data").each(function() {
    var value = $(this);
    if (map[value.text()]) {
      map[value.text()].parent('li').remove();
    }
    map[value.text()] = value;
  });
}

removeDuplicates();
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<ul>
  <li><span class="data">1</span></li>
  <li><span class="data">2</span></li>
  <li><span class="data">3</span></li>
  <li><span class="data">2</span></li>
  <li><span class="data">1</span></li>
  <li><span class="data">4</span></li>
  <li><span class="data">5</span></li>
  <li><span class="data">2</span></li>
</ul>