1

I have a table that produces data dynamically. i would like to know how to remove rows that contain the same data within <tr> elements and keep the first two <tr> elements. This question is very similar to this

<table id="test">
<tr>
<td>test1<td>
</tr>
<tr>
<td>test2<td>
</tr>
<tr>
<td>test1 a<td>
</tr>
<tr>
<td>test2 b<td>
</tr>
</table>

I would like the result to be:

<table id="test">
<tr>
<td>test1<td>
</tr>
<tr>
<td>test2<td>
</tr>
</table>
Community
  • 1
  • 1
Billy
  • 2,823
  • 3
  • 16
  • 33
  • Get a collection representing the TD's in that table: make a new list called '_uniqueValues' : loop over TD_collection and check _unique Values and if doesn't exist, add (current_loop_object) to list Else delete the (current_loop_object) from DOM. Please post relevent code/your attempt :p edit: I don't know js, sry. – ABuckau Aug 18 '16 at 13:38

1 Answers1

2

You can achieve this by looping through each td and then filtering out other td which start with the same string before removing them. Try this:

var $tds = $('#test td');
$tds.each(function() {
  var text = $(this).text();
  $tds.not(this).filter(function() {
    return $(this).text().indexOf(text) == 0;
  }).remove();
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table id="test">
  <tr>
    <td>test1<td>
  </tr>
  <tr>
    <td>test2</td>          
  </tr>
  <tr>
    <td>test1 a<td>
  </tr>
  <tr>
    <td>test2 b<td>
  </tr>
</table>
Rory McCrossan
  • 331,213
  • 40
  • 305
  • 339