3

I'm trying to display unique rows based on values of the first column.I have tried this a lot taking cue from a similar question.

    <table id="test" border="1">
    <tr>
        <td>test1</td>
        <td>test2</td>
        <td>test3</td>
    </tr>
      <tr>
          <td>test1</td>
          <td>test2</td>
          <td>test5</td>
    </tr>
    <tr>
        <td>test6</td>
        <td>test2</td>
        <td>test5</td>
    </tr>
      <tr>
              <td>test6</td>
              <td>test6</td>
              <td>test9</td>
        </tr>
    </table

>

 **BEFORE**

 test1  test2   test3
 test1  test2   test5
 test6  test2   test5
 test6  test6   test9

 **AFTER**

 test1  test2   test3  
 test6  test2   test5

Here's what I have tried using codes from a similar situation

     arr = [];
     $('#test td').each(function(){
        key = "" + $(this).index(); 
    
       if(arr.indexOf( key ) == 1)
           arr.push($.trim($(this).text()));
       else
        $(this).closest('tr').hide();    
      alert(arr[]);
      });

  **RESULT**:      

  test1 test2   test3

2 Answers 2

2

Loop each first td adding the value to a lookup object if its not not already present. If it is present, hide the current row.

var found = {};
    $('#test td:first-child').each(function(){
       var td = $(this);
       var value = td.text();
       if (found[value])
          td.closest('tr').hide();
       else
          found[value] = true;
    });

var found = {};
    $('#test td:first-child').each(function(){
       var td = $(this);
       var value = td.text();
       if (found[value])
          td.closest('tr').hide();
       else
          found[value] = true;
    });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
 <table id="test" border="1">
    <tr>
        <td>test1</td>
        <td>test2</td>
        <td>test3</td>
    </tr>
      <tr>
          <td>test1</td>
          <td>test2</td>
          <td>test5</td>
    </tr>
    <tr>
        <td>test6</td>
        <td>test2</td>
        <td>test5</td>
    </tr>
    <tr>
        <td>test6</td>
        <td>test6</td>
        <td>test9</td>
    </tr>
    </table>

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for sharing the concept. Can u suggest any javascript book for reference?
javascript.info My favourite site
1

You can also use filter to find rows to remove:

$('#test tr').filter(function() {
    var text = $(this.cells[0]).text();
    return $(this).prev('tr').filter(function() {
        return $(this.cells[0]).text() === text;
    }).length;
}).remove();

$('#test tr').filter(function() {
    var text = $(this.cells[0]).text();
    return $(this).prev('tr').filter(function() {
        return $(this.cells[0]).text() === text;
    }).length;
}).remove();
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table id="test" border="1">
    <tr>
        <td>test1</td>
        <td>test2</td>
        <td>test3</td>
    </tr>
    <tr>
        <td>test1</td>
        <td>test2</td>
        <td>test5</td>
    </tr>
    <tr>
        <td>test6</td>
        <td>test2</td>
        <td>test5</td>
    </tr>
    <tr>
        <td>test6</td>
        <td>test6</td>
        <td>test9</td>
    </tr>
</table>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.