我有一个像这样的div列表
<div data-id="001">Blah Blah Blah</div>
<div data-id="001">Blah Blah Blah</div>
<div data-id="002">Blah Blah Blah</div>
<div data-id="002">Blah Blah Blah</div>
<div data-id="002">Blah Blah Blah</div>
<div data-id="003">Blah Blah Blah</div>
<div data-id="003">Blah Blah Blah</div>
<div data-id="004">Blah Blah Blah</div>
是否可以扫描此列表并删除包含重复数据的所有项目,以便我最终得到更多类似的内容:
<div data-id="001">Blah Blah Blah</div>
<div data-id="002">Blah Blah Blah</div>
<div data-id="003">Blah Blah Blah</div>
<div data-id="004">Blah Blah Blah</div>
答案 0 :(得分:11)
当您列举所有项目时,只需跟踪您已经找到的任何项目。
var found = {};
$('[data-id]').each(function(){
var $this = $(this);
if(found[$this.data('id')]){
$this.remove();
}
else{
found[$this.data('id')] = true;
}
});
答案 1 :(得分:1)