我试图在过滤器中添加两个条件,但只有一个有效。第一个条件检查单词之间是否有空格,第二个条件检查words.length 是否大于给定的最小长度。
如果字符串是"hello world"
然后我需要在分割它时得到["hello", "world"]
。相反,我得到的是["hello", "", "", "", "world"]
let wordsLength = sumOfSentence.split(" ");
let longWords = wordsLength.filter(function(sumOfWord){
//check if the words length is bigger than the minimum length
//check if it has extra empty spaces
if(sumOfWord !== "") return sumOfWord.length >= minLength
});
似乎您想过滤 sumOfWord 不为空且其长度大于 minLength 的情况。 @Barmar 建议您好的解决方案,使用以下代码。
let wordsLength = sumOfSentence.split(" ");
let longWords = wordsLength.filter(function(sumOfWord){
return ((sumOfWord.trim() != '') && sumOfWord.length >= minLength)
});
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)