admin管理员组文章数量:1321439
How can I count the distinct table rows by table column?
Example:
<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>
I want Out put:
css : 3
jQuery : 2
here I don't know what technology is e in that cell but, I need to get the no of rows by the technology
Thanks in advance
How can I count the distinct table rows by table column?
Example:
<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>
I want Out put:
css : 3
jQuery : 2
here I don't know what technology is e in that cell but, I need to get the no of rows by the technology
Thanks in advance
Share Improve this question edited Jan 20, 2014 at 16:49 Abdullah Ahmed 731 silver badge13 bronze badges asked Jan 20, 2014 at 16:45 sunnysunny 4891 gold badge9 silver badges19 bronze badges 1- Check out this question: stackoverflow./questions/15411966/… I think that it could help you :) – Victor Commented Jan 20, 2014 at 16:50
3 Answers
Reset to default 7var 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
// }
Call function below on load. Check this jsFiddle out - http://jsfiddle/pqzKa/2/
function count()
{
var map = {};
$("tbody tr td:nth-child(2)").each(function(i, val){
var key = $(val).text();
if (key in map)
map[key]=map[key] + 1;
else
map[key] = 1;
});
var result='';
for(var key in map)
{
result += key + ':' + map[key] + '\n';
}
alert(result);
}
The function builds a hashmap called map. It then iterates over the second cell of each tr in tbody and checks to see if it is present in the map. If not, it adds it to the hashmap with a count of 1. If yes, it increments the count by 1. I hope this helps.
Another route might be to write a more generic function for counting people who use certain technologies.
Assuming your using jQuery and your table has an id 'tech' you could do something like this
counter = function(textMatch){
count=0;
//loop through all <tr> s
$('#tech').find('tbody tr').each(function( index ) {
//if second <td> contains matching text update counter
if($($(this).find('td')[1]).text() == textMatch){
count++
}
});
return count;
}
console.log('Number of people using CSS is ' + counter('css'));
console.log('Number of people using CSS is ' + counter('jQuery'));
//and so on
本文标签: javascriptjQuery How to count the no of rows in table by distinct column valueStack Overflow
版权声明:本文标题:javascript - jQuery How to count the no of rows in table by distinct column value - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742101125a2420805.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论