1

I need to compare two HTML tables' rows assuming that data in first cell can be duplicated but data in second cell is always unique. I need to find whether first cell AND second cell in table1 is the same as data in first cell AND second cell in table2 for instance:

Table1:

<Table>
    <tr>
        <td>123</td>
        <td>321</td>
    </tr>
    <tr>
        <td>545</td>
        <td>345</td>
    </tr>
    <tr>
        <td>0</td>
        <td>312</td>
    </tr>
    <tr>
        <td>123</td>
        <td>323331</td> 
    </tr>
</Table>

Second table:

<table>
    <tr>
        <td>545</td>
        <td>345</td>
    </tr>
    <tr>
        <td>545</td>
        <td>3122</td>
    </tr>
    <tr>
        <td>123</td>
        <td>321</td>
    </tr>
</table>

The result of this should be:

123 321 - good, do nothing 545 345 - good, do nothing 545 3122 - wrong its not in table1 <-

Here's what I've got so far...

$('#runCheck').click(function(){
        var firstTable = $('#firstDiv table tr');
        var secondTable = $('#secDiv table tr');

        $(secondTable).each(function(index){
            var $row = $(this);
            var secTableCellZero = $row.find('td')[0].innerHTML;
            var secTableCellOne = $row.find('td')[1].innerHTML;

            $(firstTable).each(function(indexT){


                if ($(this).find('td')[0].innerHTML === secTableCellZero){
                    if ($(this).find('td')[1].innerHTML !== secTableCellOne){
                        $('#thirdDiv').append("first: " + secTableCellZero + " second: " + secTableCellOne+"<br>");

                    }

                }

            });

        });
     });  

Where am I going it wrong?

Just to clarify once again:

2nd table says : row1 - john|likesCookies row2 - peter|likesOranges

1st table says : row1 - john|likesNothing row2 - john|likesCookies row3 - steward|likesToTalk row4 - peter|likesApples

now it should say : john - value okay peter - value fail.

a lot alike =VLOOKUP in excel

Pat
  • 29
  • 1
  • 7
  • 1
    Do both tables have same number of rows ? – AsgarAli Feb 04 '16 at 21:50
  • no they dont, second table has always less number of rows. It's sort of "check if correctly filled in". – Pat Feb 04 '16 at 21:54
  • You are looping all of first table. Is the idea to find match anywhere in first table? Or just same row index? – charlietfl Feb 04 '16 at 21:54
  • According to "assuming that data in first cell can be duplicated" this condition is not needed: `if ($(this).find('td')[0].innerHTML === secTableCellZero){` – stackErr Feb 04 '16 at 21:54
  • The idea is to find a match anywhere in first table, and match for first cell has to be there if it's not there then i need to get a message back. Then if it's there, the data from 2nd table 1st cell and 2nd cell has to be the same as data from 1st table 1st cell and 2nd cell – Pat Feb 04 '16 at 21:57
  • Okay basically, 2nd table row1 cell1 = 1 2nd table row1 cell2 = 2 2nd table row2 cell1 = 1 2nd table row2 cell2 = 4 1st table row1 cell1 = 1 1st table row1 cell2 = 2 <-- all good 1st table row2 cell1 = 1 1st table row2 cell2 = 43 <-- bad – Pat Feb 04 '16 at 22:02
  • Very confusing. Can't understand your requirement – AsgarAli Feb 04 '16 at 22:03
  • http://postimg.org/image/7pd6rdwbr/ – Pat Feb 04 '16 at 22:08
  • ^ **NSFW** link. Consider posting on a better image hosting site – stackErr Feb 04 '16 at 22:09
  • @Pat so all elements(rows) in Table 1 need to be in Table 2? And the order they appear in does not matter? – stackErr Feb 04 '16 at 22:12
  • https://img42.com/Qj3E0 order does not matter at all. I just need to make sure that whatever i paste to table2 is going to be compared against table1 and checked whether cell1 and cell2 have same values – Pat Feb 04 '16 at 22:14
  • @Pat Why last row of first is not bad ? because that pair also does not exist in second table as per above mentioned image – AsgarAli Feb 04 '16 at 22:20
  • because it does not have to exist in 2nd table it has to exist in 1st. Whatever is pasted into 2nd table has to exist in first not necessarily the other way around. – Pat Feb 04 '16 at 23:14

3 Answers3

0

If I understand your requirements, it would be easier to read the first table and store the couples as strings: 123/321, 545/345, etc...

Than you can read the second table and remove from the first list all the rows found in both. What remains in the list are couples that do not match.

Marco Altieri
  • 3,726
  • 2
  • 33
  • 47
0

From purely an efficiency standpoint if you loop through the first table just once and create an object using the first cell value as keys and an array of values for second cells, you won't have to loop through that table numerous times

this then makes the lookup simpler also

var firstTable = $('#firstDiv table tr');
var secondTable = $('#secDiv table tr');

var firstTableData = {}
firstTable.each(function() {
  var $tds = $(this).find('td'),
    firstCellData = $tds.eq(0).html().trim(),
    secondCellData == $tds.eq(1).html().trim();

  if (!firstTableData[firstCellData]) {
    firstTableData[firstCellData] = []
  }
  firstTableData[firstCellData].push(secondCellData)
})

$(secondTable).each(function(index) {
  var $tds = $(this).find('td');
  var secTableCellZero = $tds.eq(0).html().trim();
  var secTableCellOne = $tds.eq(1).html().trim();

  if (!firstTableData.hasOwnProperty(secTableCellZero)) {
    console.log('No match for first cell')
  } else if (!firstTableData[secTableCellZero].indexOf(secTableCellOne) == -1) {
     console.log('No match for second cell')
  }
});

I'm not sure what objective is when matches aren't found

charlietfl
  • 170,828
  • 13
  • 121
  • 150
  • If that's what was needed to get it done then I'm still too newbie for it :( but i'll do my best do work more with objects and arrays. – Pat Feb 04 '16 at 22:18
  • 1
    doesn't mean there can't be duplicates but criteria is not very clear – charlietfl Feb 04 '16 at 22:19
0

Check this working fiddle : here

I've created two arrays which store values in each row of tables 1 and 2 as strings. Then I just compare these two arrays and see if each value in array1 has a match in array 2 using a flag variable.


Snippet :

$(document).ready(function() {
  var table_one = [];
  var table_two = [];
  $("#one tr").each(function() {
    var temp_string = "";
    count = 1;
    $(this).find("td").each(function() {
      if (count == 2) {
        temp_string += "/";
      }
      temp_string = temp_string + $(this).text();
      count++;
    });
    table_one.push(temp_string);
  });
  $("#two tr").each(function() {
    var temp_string = "";
    count = 1;
    $(this).find("td").each(function() {
      if (count == 2) {
        temp_string += "/";
        temp_string = temp_string + $(this).text();
      } else {
        temp_string = temp_string + $(this).text();
      }
      count++;
    });
    table_two.push(temp_string);
  });
  var message = "";
  for (i = 0; i < table_two.length; i++) {
    var flag = 0;
    var temp = 0;
    table_two_entry = table_two[i].split("/");
    table_two_cell_one = table_two_entry[0];
    table_two_cell_two = table_two_entry[1];
    for (j = 0; j < table_one.length; j++) {
      table_one_entry = table_one[j].split("/");
      table_one_cell_one = table_one_entry[0];
      table_one_cell_two = table_one_entry[1];
      console.log("1)" + table_one_cell_one + ":" + table_one_cell_two);
      if (table_two_cell_one == table_one_cell_one) {
        flag++;
        if (table_one_cell_two == table_two_cell_two) {
          flag++;
          break;
        } else {
          temp = table_one_cell_two;
        }
      } else {}
    }
    if (flag == 2) {
      message += table_two_cell_one + " " + table_two_cell_two + " found in first table<br>";
    } else if (flag == 1) {
      message += table_two_cell_one + " bad - first table has " + temp + "<br>";
    } else if (flag == 0) {
      message += table_two_cell_one + " not found in first table<br>";
    }
  }
  $('#message').html(message);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<hr>
<table id="one">
  <tr>
    <td>123</td>
    <td>321</td>
  </tr>
  <tr>
    <td>545</td>
    <td>345</td>
  </tr>
  <tr>
    <td>0</td>
    <td>312</td>
  </tr>
  <tr>
    <td>123</td>
    <td>323331</td>
  </tr>
</table>
<hr>
<table id="two">
  <tr>
    <td>545</td>
    <td>345</td>
  </tr>
  <tr>
    <td>545</td>
    <td>3122</td>
  </tr>
  <tr>
    <td>123</td>
    <td>321</td>
  </tr>
</table>
<hr>
<div id="message">
</div>
</div>
stark
  • 2,246
  • 2
  • 23
  • 35
  • I believe i can do something out of that, that's not exactly it but yeah it's close enough for me I should be able to make it work for me. Thank you so much Sir. Hopefully i will be able to re-make it a little – Pat Feb 04 '16 at 22:37
  • @Pat : What exactly is your required output and how is it different from the output in my snippet? – stark Feb 04 '16 at 22:39
  • 545 3122 should return 545 3122 is not in first table, okay, we have 545 as first cell in 2nd table. Then lets assume we have 545 in first table first cell but we have a different value in 1st table 2nd cell than 2nd table 2nd cell it. That's when it should actually tell me something's wrong – Pat Feb 04 '16 at 22:42
  • So basically you want to know if every value in the second table has a match in the first table ? and if not then you want to display which value in second table is not present in the first table right ? – stark Feb 04 '16 at 22:44
  • Sorry Sirs I'm fighting with it all day now I feel tired :) - https://img42.com/VT22k First i want to find whether value from 2nd table 1st cell exists in 1st table 1st cell. If it does i need to check whether value from the same row (row might be different in each table, remember order doesnt matter)2nd cell is equal in both tables – Pat Feb 04 '16 at 22:48
  • @Pat : Check my edited fiddle in the answer : the code now cross-checks both tables against one anothr and then displays the combined result. – stark Feb 04 '16 at 22:55
  • It's all about returning data from 1st table ONLY if match for 1st cell was found from 2nd table but 2nd table has different cell2 than has table1 cell2 so if you have table1 john | likesCookies and table2 john|likesIceCream it should say - i found john but it doesn't say likesCookies it says likesIceCream. It might be that there is another value john|likesCookies in 1st table and then it shouldn't do anything. It's all a matter of checking whether 2nd cell for john is the same as 2nd cell in 1st table. Sorry I think I should go to bed I can't think clearly now it blew my mind – Pat Feb 04 '16 at 23:10
  • @Pat : Check my latest fiddle in my answer : it now outputs the result exactly as the way you described , is this what you were saying ? – stark Feb 04 '16 at 23:39
  • I was already checking your fiddle updates live :) I believe that's exactly what i was trying to say. Sorry for this huge struggle i was trying to figure it out myself whole day without any arrays. I definitely need to work harder on the language as I'm not certain about all opportunities it gives me. Thank you so much for the effort, hopefully someday I'll grow to be a coder ; ( Can i somehow close the topic? – Pat Feb 04 '16 at 23:47
  • No problem :),So is your question answered then ? Mark it so if it is. – stark Feb 04 '16 at 23:49
  • My question is answered Sir, by Mark it you mean click this tick button so it lights green? I'm just learning please forgive me :) – Pat Feb 04 '16 at 23:51
  • Yes, you mark the tick whenever a question is answered and you want to accept that answer for the question. – stark Feb 04 '16 at 23:52
  • All right, i just did. Thank you so much and sorry for the hustle. I'll definitely try to do better on this next time. You guys have a good night. – Pat Feb 04 '16 at 23:54