简体   繁体   中英

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 !

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM