Fazil Mir Fazil Mir - 11 days ago 4
HTML Question

How to hide TR if its values are similar to another TR in jquery?

I want to hide the TR if its first TD value is similar to another TRs TD value.

Eg:

<table>
<tr>
<td>123</td>
<td>Example 1</td>
</tr>
<tr>
<td>789</td>
<td>Example 2</td>
</tr>
<tr>
<td>123</td>
<td>Example 3</td>
</tr>
</table>


What I want:

<table>
<tr>
<td>123</td>
<td>Example 1</td>
</tr>
<tr>
<td>789</td>
<td>Example 2</td>
</tr>
</table>


Can anybody tell me how to do this?

Any help would be highly appreciated.

Answer

You could create a map of the text in the first TD, and then filter based on that

var trs = $('table tr'),
    map = trs.map(function() { return $(this).find('td:first').text().trim() }).get();

trs.filter(function(i) {
  return map.indexOf( $(this).find('td:first').text().trim() ) !== i;
}).hide();
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table>
  <tr>
    <td>123</td>
    <td>Example 1</td>
  </tr>
  <tr>
    <td>789</td>
    <td>Example 2</td>
  </tr>
  <tr>
    <td>123</td>
    <td>Example 3</td>
  </tr>
</table>

Comments