JQuery:删除重复元素?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2822962/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 14:20:02  来源:igfitidea点击:

JQuery: Remove duplicate elements?

jqueryduplicates

提问by Keith Donegan

Say I have a list of links with duplicate values as below:

假设我有一个包含重复值的链接列表,如下所示:

<a href="#">Book</a>
<a href="#">Magazine</a>
<a href="#">Book</a>
<a href="#">Book</a>
<a href="#">DVD</a>
<a href="#">DVD</a>
<a href="#">DVD</a>
<a href="#">Book</a>

How would I, using JQuery, remove the dups and be left with the following for example:

我将如何使用 JQuery 删除重复项并留下以下内容,例如:

<a href="#">Book</a>
<a href="#">Magazine</a>
<a href="#">DVD</a>

Basically I am looking for a way to remove any duplicate values found and show 1 of each link.

基本上我正在寻找一种方法来删除找到的任何重复值并显示每个链接的 1 个。

回答by interjay

var seen = {};
$('a').each(function() {
    var txt = $(this).text();
    if (seen[txt])
        $(this).remove();
    else
        seen[txt] = true;
});

Explanation:

解释:

seenis an object which maps any previously seen text to true. It functions as a setcontaining all previously seen texts. The line if (seen[txt])checks to see if the text is in the set. If so, we've seen this text before, so we remove the link. Otherwise, this is a link text we see for the first time. We add it to the set so that any further links with the same text will be removed.

seen是一个将任何先前看到的文本映射到 的对象true。它作为一个包含所有以前看到的文本的集合。该行if (seen[txt])检查文本是否在集合中。如果是这样,我们之前已经看过这段文字,所以我们删除了链接。否则,这是我们第一次看到的链接文本。我们将其添加到集合中,以便删除具有相同文本的任何进一步链接。

An alternative way to represent a set is to use an array containing all values. However, this would make it much slower since to see if a value is in the array we'd need to scan the entire array each time. Looking up a key in an object using seen[txt]is very fast in comparison.

表示集合的另一种方法是使用包含所有值的数组。但是,这会使其变慢,因为要查看数组中是否有值,我们每次都需要扫描整个数组。seen[txt]相比之下,使用 using 在对象中查找键非常快。

回答by Vladimir

Use jQuery method $.unique()

使用 jQuery 方法$.unique()

Detail see on http://api.jquery.com/jQuery.unique/

详情见http://api.jquery.com/jQuery.unique/

回答by coolnalu

// use an object as map
var map = {};
$("a").each(function(){
    var value = $(this).text();
    if (map[value] == null){
        map[value] = true;
    } else {
        $(this).remove();
    }
});

回答by Ben Muircroft

@interjay @Georg Fritzsche

@interjay @Georg Fritzsche

Your fix didn't work in my case so I build a different version:

您的修复在我的情况下不起作用,所以我构建了一个不同的版本:

var seen='';
   $('a').each(function(){
        var see=$(this).text();
        if(seen.match(see)){
            $(this).remove();}
        else{
            seen=seen+$(this).text();
        }
    });

Hopes this provides someone else with a valid alternative short fix just in case.

希望这可以为其他人提供有效的替代短期修复以防万一。

回答by Saleh

$(document).ready(function(){
   $("select").each(function () {
       var selectedItem = $(this).find('option').filter(':selected').text();
       var selectedItemValue = $(this).find('option').filter(':selected').val();
       $(this).children("option").each(function(x){
           if(this.text == selectedItem && $(this).val() != selectedItemValue) {
               $(this).remove();
            }
        });
    }); 
});

回答by Luis Eduardo Telaya Escobedo

Nice solution people. Here is mine

很好的解决方案的人。这是我的

for (i = 0; i < $('li').length; i++) {
  text = $('li').get(i);
  for (j = i + 1; j < $('li').length; j++) {
    text_to_compare = $('li').get(j);
    if (text.innerHTML == text_to_compare.innerHTML) {
      $(text_to_compare).remove();
      j--;
      maxlength = $('li').length;
    }
  }
}

Greetings

你好

回答by Anders Mattson

A quick and easy way would be

一种快速简便的方法是

$("a").????????each(function(){
    if($(this).parent().length)
        $("a:contains('" + $(this).html() + "')").not(this).remove();
});?

回答by user32285

$('.photo').each(function (index) { 
    if (index > 0) { 
        $(this).remove(); 
    } 
});