jQuery如何通过不同的列值计算表中的行数 [英] jQuery How to count the no of rows in table by distinct column value

查看:80
本文介绍了jQuery如何通过不同的列值计算表中的行数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何按表格列计数不同的表格行?

How can I count the distinct table rows by table column?

示例:

<table>
   <thead> 
      <tr>
        <th>NAME</th>
        <th>TECHNOLOGY</th>
     </tr>
   </thead>
   <tbody>  
     <tr>
        <td>john</td>
        <td>jQuery</td>
    </tr>
    <tr>
       <td>mark</td>
       <td>css</td>
    </tr>
    <tr>
       <td>blah</td>
       <td>css</td>
    </tr>
    <tr>
       <td>abc</td>
       <td>css</td>
    </tr>
    <tr>
       <td>xyz</td>
       <td>jQuery</td>
    </tr>
  </tbody>
</table>

我要放出:

css : 3
jQuery : 2

在这里,我不知道该单元格中使用了什么技术,但是,我需要根据该技术获得行数

here I don't know what technology is come in that cell but, I need to get the no of rows by the technology

预先感谢

推荐答案

var byTechnology = {};

$("table tbody td:nth-child(2)").each(function () {
    var tech = $.trim( $(this).text() );

    if ( !(byTechnology.hasOwnProperty(tech)) ) {
        byTechnology[tech] = 0;
    }

    byTechnology[tech]++;
});

console.log(byTechnology);
// {
//     css: 3,
//     jQuery: 2
// }

这篇关于jQuery如何通过不同的列值计算表中的行数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆