문제

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

도움이 되었습니까?

해결책

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)

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top