在JS中计数唯一字 [英] Count unique words in table with JS
本文介绍了在JS中计数唯一字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想使用jQuery或者SQL来计算表中的唯一字数。我现在没有线索如何做到这一点。所以请原谅我,当我不给你我的尝试。
I would like to calculate unique words count in a table with jQuery or maybe SQL. I have no clue at the moment how to do this. So please forgive me when i dont give you my tries.
这是表:
<table>
<tr class="odd">
<td>450€ Job</td>
<td>Aachen</td>
</tr>
<tr class="even">
<td>500€ Job</td>
<td>Berlin</td>
</tr>
<tr class="even">
<td>500€ Job</td>
<td>Berlin</td>
</tr>
</table>
我想得到这样的东西:
Aachen (1)
Berlin (2)
你有什么想法吗?非常感谢!
Do you have any ideas? Thank you very much!
最好的祝福
推荐答案
将使用JavaScript。此代码计算整个表的字。如果您需要为该行运行它,您应该适当地修改它。
Here how I would do it using JavaScript. This code calculates words for whole table. If you need to run it for the row, you should modify it appropriately.
var words = [];
var uniqueWords = [];
$("td").each(function(){ words.push($(this).text()) });
$(words).each(function(){
for(var i = 0; i < uniqueWords.length; i++){
var current = uniqueWords[i];
if(current.word.toString() == this.toString()){
current.count++;
return;
}
}
uniqueWords.push({count: 1, word: this});
});
$(uniqueWords).each(function(){
console.log(this.count + " " + this.word);
});
http://jsfiddle.net/4LSgC/ (请参阅控制台)
这篇关于在JS中计数唯一字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文