Reputation: 401
I have a table in my HTML,
<table>
<tr><td>1</td><td>2</td><td>3</td></tr>
<tr><td>a</td><td>2</td><td>c</td></tr>
<tr><td>x</td><td>2</td><td>3</td></tr>
<tr><td>1</td><td>2</td><td>3</td></tr>
</table>
And i wanted to find duplicate contents in the first and the second <td>
elements contents.
This is what i got so far:
$(function() {
$("table tr").each(function() {
});
});
I m stuck on selecting the first and the second element and comparing them. Do i just add a class or use jquery selector?
Upvotes: 1
Views: 7026
Reputation: 12682
just store the combination of the first two values as properties in an object
$(function() {
var el = {};
$("table tr").each(function() {
// get row
var row = $(this);
// get first and second td
var first = row.find('td:first-child').val();
var second = row.find('td:nth-child(2)').val();
// if exists, remove the tr
if(el[first + second]) {
$(this).remove())
}
else {
// if it does not exist, add it with some random val
el[first + second] = 1;
}
});
});
Upvotes: 2
Reputation:
This is how you find the duplicates in a column, if I understood.
You can use instead of .addClass
, .remove()
if you want to remove it.
http://jsbin.com/wenuwewide/edit?js,console,output
$('tr').each(function() {
$(this).find('td').each(function(i) {
if(values[i].indexOf($(this).text()) > -1) {
$(this).addClass('duplicate');
}
values[i].push($(this).text());
});
});
Upvotes: 3