简体   繁体   English

jQuery / Javascript相互比较两个表

[英]jQuery/Javascript compare two tables against each other

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. 我需要比较两个HTML表的行,假设第一个单元格中的数据可以重复,但是第二个单元格中的数据始终是唯一的。 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中的第一个单元格和第二个单元格是否与table2 2中的第一个单元格和第二个单元格中的数据相同:

Table1: 表格1:

<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 <- 123321-好,什么也不做545 345-好,什么都不做545 3122-错误,不在表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 第二张表说:row1-john | likesCookies row2-peter | likesOranges

1st table says : row1 - john|likesNothing row2 - john|likesCookies row3 - steward|likesToTalk row4 - peter|likesApples 第一张桌子说:row1-john | likesNothing row2-john | likesCookies row3-管家| likesToTalk row4-peter | likesApples

now it should say : john - value okay peter - value fail. 现在它应该说:约翰-价值确定彼得-价值失败。

a lot alike =VLOOKUP in excel 在Excel中非常相似= VLOOKUP

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... 如果我了解您的要求,那么阅读第一张表并将夫妇存储为字符串会更容易:123 / 321、545 / 345等。

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. 列表中剩下的是不匹配的夫妇。

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 我不确定找不到匹配项的目的是什么

Check this working fiddle : here 检查此工作提琴: 这里

I've created two arrays which store values in each row of tables 1 and 2 as strings. 我创建了两个数组,它们将表1和2的每一行中的值存储为字符串。 Then I just compare these two arrays and see if each value in array1 has a match in array 2 using a flag variable. 然后,我只比较这两个数组,并使用标志变量查看array1中的每个值在数组2中是否具有匹配项。


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> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM