Search code examples
javascriptarraysbookmarklet

Ignore words less than or equal 3 javascript array


I'm building my own boorkmarklet for analyze the words in the current page, currently it's working good, but I would like filter the words and just show the words longer than 3 letters, I'm new with javascript but here is my code:

    var sWords = document.body.innerText.toLowerCase().trim().replace(/[,;.]/g,'').split(/[\s\/]+/g).sort();
    // count duplicates
    var iWordsCount = sWords.length;

// array of words to ignore
var ignore = ['and','the','to','a','of','for','as','i','with','it','is','on','that','this','can','in','be','has','if'];
ignore = (function(){
    var o = {};
    var iCount = ignore.length;
    for (var i=0;i<iCount;i++){
        o[ignore[i]] = true;
    }
    return o;
}());

thanks for the time !


Solution

  • You can use filter function :

    function greaterThanThree(element){
        return element.length > 3;
    }
    
     var longer_words = ['f','as','i','with','on','that','this','can','has','if'].filter(greaterThanThree);
    
    //Will return ["with", "that", "this"] 
    

    Hope this helps.