JSFiddle - React, Tailwind, and code Playground

by iori horigome

HTML

<table id="testTable" border="1">
<tbody><tr>
<th>関東</th>
<td>東京</td>
</tr>
<tr>
<th>関東</th>
<td>千葉</td>
</tr>
<tr>
<th>関東</th>
<td>神奈川</td>
</tr>
<tr>
<th>近畿</th>
<td>大阪</td>
</tr>
<tr>
<th>近畿</th>
<td>兵庫</td>
</tr>
<tr>
<th>近畿</th>
<td>京都</td>
</tr>
</tbody></table>

JavaScript

$(document).ready(function(){
 $('#testTable').each(function () {
  var pre_element = null;
  var col_num = 0;
  $(this).find('tr').each(function () {
   var now_th = $(this).find('th').eq( col_num );
   if (pre_element == null) {
    pre_element = now_th;
   } else if (now_th.text() == pre_element.text()) {
    now_th.remove();
    if (pre_element.attr('rowspan') == null) pre_element.attr('rowspan', 1);
     pre_element.attr('rowspan', parseInt(pre_element.attr('rowspan'),10) + 1);
    } else {
     pre_element = now_th;
    }
  });
 });
});