Question

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

Was it helpful?

Solution

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)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top