[Solved] Count unique words in table with JS [closed]


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)

1

solved Count unique words in table with JS [closed]