Use your seen
object as a counter instead of just setting true
, then check the count on each row iteration.
I’m not 100% clear on what you expect the behavior to be so am appending any rows to duplicate table that already have two matches
function reoveDuplicateFramTable() {
$("#duplicateData").empty();
var seen = {};
$('#Data tr').each(function() {
var txt = $(this).text().trim();
seen[txt] = (seen[txt] || 0) + 1
if (seen[txt] > 2) {
$("#duplicateData").append(this)
}
});
}
reoveDuplicateFramTable()
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<table>
<tbody id="Data">
<tr>
<th>1</th>
</tr>
<tr>
<th>1</th>
</tr>
<tr>
<th>1</th>
</tr>
<tr>
<th>2</th>
</tr>
<tr>
<th>2</th>
</tr>
<tr>
<th>3</th>
</tr>
</tbody>
</table>
<h3>Duplicates</h3>
<table>
<tbody id="duplicateData">
<tr>
<th>1</th>
</tr>
<tr>
<th>2</th>
</tr>
</tbody>
</table>
I have two table one for the data and the second one for duplicate data in first table
Imagen that inside first table :
<tbody id="Data">
<tr> <th>1</th> </tr>
<tr> <th>1</th> </tr>
<tr> <th>1</th> </tr>
<tr> <th>2</th> </tr>
<tr> <th>2</th> </tr>
<tr> <th>3</th> </tr>
</tbody>
I use this function to remove duplicate data
function reoveDuplicateFramTable(){
$("#duplicateData").empty();
var seen = {};
$('#Data tr').each(function() {
var txt = $(this).text();
if (seen[txt]){ $("#duplicateData").append($(this)) }
else{ seen[txt] = true }
});}
and the second table will be like this:
<tbody id="duplicateData">
<tr> <th>1</th> </tr>
<tr> <th>2</th> </tr>
</tbody>
how about if i wand just to remove duplicate data if they repeated 3 time?
the second table be like this:
<tbody id="duplicateData">
<tr> <th>1</th> </tr>
</tbody>
is that possible in Jquery?
check :contains(value) in jquery and calculate output length for each element found.
sorry .. I’am beginner could you explain that by edit in my code?
Code wise this appears simpler than a tracking object but all those extra DOM queries are a lot more expensive, especially on larger table
@charlietfl thanks, you are absolutely right but give the fact that he is a beginner I think solution will work for test projects.