忽略小于或等于 3 javascript 数组的单词

Ignore words less than or equal 3 javascript array

我正在构建自己的 boorkmarklet 来分析当前页面中的单词,目前它运行良好,但我想过滤单词并只显示长度超过 3 个字母的单词,我是 javascript 但这是我的代码:

    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;
}());

感谢您的宝贵时间!

你可以使用filter函数:

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"] 

希望对您有所帮助。