Count unique words in table with JS [closed]

岁酱吖の 提交于 2019-12-13 09:03:23

问题


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.

This is the table:

<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>

I would like to get somthing like this:

Aachen (1)
Berlin (2)

Do you have any ideas? Thank you very much!

Best regards


回答1:


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/ (see console)



来源:https://stackoverflow.com/questions/20467276/count-unique-words-in-table-with-js

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!