删除每个函数中的重复项
Delete duplicate Item in Each function
我想存档,我删除了每个项目中的所有重复项。
所以我 div 有以下项目:
<div class="each">
<div>Test2</div>
<div>Test</div>
<div>Test</div>
</div>
<div class="each">
<div>Test2</div>
<div>Test</div>
<div>Test</div>
</div>
我的 Jquery 到目前为止:
$('.dropPlace').each(function(index){
console.log($(this).text());
});
所以我想检查 每个 div
是否存在两个重复的文本。并且仅在 div
.
中删除了重复的一次
所以结果是这样的:
<div class="each">
<div>Test2</div>
</div>
<div class="each">
<div>Test2</div>
</div>
这是一个工作演示。
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script>
$(document).ready(function () {
var map = {};
$("#remove").click(function () {
// use an object as map
var map = {};
$(".dropPlace").each(function () {
var value = $(this).text();
if (map[value] == null) {
map[value] = true;
} else {
$(this).remove();
}
});
});
});
</script>
</head>
<body>
<div class="each">
<div class="dropPlace">Test2</div>
<div class="dropPlace">Test</div>
<div class="dropPlace">Test</div>
</div>
<div class="each">
<div class="dropPlace">Test2</div>
<div class="dropPlace">Test</div>
<div class="dropPlace">Test</div>
</div>
<button id="remove">remove</button
</body>
</html>
以下肯定可以优化,但它会给出您要求的结果:
<div class="each">
<div>Test2</div>
</div>
<div class="each">
<div>Test2</div>
</div>
$('.each').each(function() {
const textAr = [];
let parent = $(this);
parent.children('div').each(function() {
let _this = $(this), elText = _this.text();
if(textAr.includes(elText)) {
parent.children('div').each(function() {
let _child = $(this);
if(_child.text() == elText){
_child.remove();
}
})
}
else textAr.push(elText)
});
});
.each {
margin-bottom: 1em;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="each">
<div>Test2</div>
<div>Test</div>
<div>Test</div>
</div>
<div class="each">
<div>Test2</div>
<div>Test</div>
<div>Test</div>
</div>
我想存档,我删除了每个项目中的所有重复项。
所以我 div 有以下项目:
<div class="each">
<div>Test2</div>
<div>Test</div>
<div>Test</div>
</div>
<div class="each">
<div>Test2</div>
<div>Test</div>
<div>Test</div>
</div>
我的 Jquery 到目前为止:
$('.dropPlace').each(function(index){
console.log($(this).text());
});
所以我想检查 每个 div
是否存在两个重复的文本。并且仅在 div
.
所以结果是这样的:
<div class="each">
<div>Test2</div>
</div>
<div class="each">
<div>Test2</div>
</div>
这是一个工作演示。
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script>
$(document).ready(function () {
var map = {};
$("#remove").click(function () {
// use an object as map
var map = {};
$(".dropPlace").each(function () {
var value = $(this).text();
if (map[value] == null) {
map[value] = true;
} else {
$(this).remove();
}
});
});
});
</script>
</head>
<body>
<div class="each">
<div class="dropPlace">Test2</div>
<div class="dropPlace">Test</div>
<div class="dropPlace">Test</div>
</div>
<div class="each">
<div class="dropPlace">Test2</div>
<div class="dropPlace">Test</div>
<div class="dropPlace">Test</div>
</div>
<button id="remove">remove</button
</body>
</html>
以下肯定可以优化,但它会给出您要求的结果:
<div class="each">
<div>Test2</div>
</div>
<div class="each">
<div>Test2</div>
</div>
$('.each').each(function() {
const textAr = [];
let parent = $(this);
parent.children('div').each(function() {
let _this = $(this), elText = _this.text();
if(textAr.includes(elText)) {
parent.children('div').each(function() {
let _child = $(this);
if(_child.text() == elText){
_child.remove();
}
})
}
else textAr.push(elText)
});
});
.each {
margin-bottom: 1em;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="each">
<div>Test2</div>
<div>Test</div>
<div>Test</div>
</div>
<div class="each">
<div>Test2</div>
<div>Test</div>
<div>Test</div>
</div>