Javascript 基于数组关键字计算在文本字符串中找到的字数

Javascript 基于数组关键字计算在文本字符串中找到的字数,javascript,arrays,Javascript,Arrays,我编写了下面的代码,虽然它可以工作,但效率非常低,并且可能会受到批评/返工。主要的问题是,扩大规模是一场噩梦 function wordCount(layers) { var countNum = '3' if (layers.includes('Text1') && layers.includes('Text2') && layers.includes('Text3,')) { return countNum - 3;

我编写了下面的代码,虽然它可以工作,但效率非常低,并且可能会受到批评/返工。主要的问题是,扩大规模是一场噩梦

function wordCount(layers) {
    var countNum = '3'

    if (layers.includes('Text1') && layers.includes('Text2') && layers.includes('Text3,')) {
        return countNum - 3;
    } else if (layers.includes('Text1') && layers.includes('Text2')) {
        return countNum - 2;
    } else if (layers.includes('Text1') && layers.includes('Text3,')) {
        return countNum - 2;
    } else if (layers.includes('Text2') && layers.includes('Text3,')) {
        return countNum - 2;
    } else if (layers.includes('Text1') || layers.includes('Text2') || layers.includes('Text3,')) {
        return countNum - 1;
    }
    return countNum;
} 
console.log(wordCount('Layer 1, Text1')); //return 2
下面是我到目前为止所做的尝试,但还没有弄清楚如何从字符串中找到的数组中计算字数

function wordCount() {
    let keywords = ['Text1', 'Text2', 'Text3', 'Text4', 'Text5']
    let textString = 'Layer1, Text1, Text5'
    let count = 0
    for (var i = 0; i < keywords.length; i++) {
        console.log(keywords[i].length); // when running code as-is return 5.
        return textString.split(", ").length;  
    }
}
console.log(wordCount());; // when running code as-is return 3.
//I'm expecting the return to be 2.
函数wordCount(){
let关键字=['Text1','Text2','Text3','Text4','Text5']
设textString='Layer1,Text1,Text5'
让计数=0
对于(var i=0;i

非常感谢您的帮助!提前谢谢你

循环遍历关键字,如果匹配
则增加一个计数器

函数字数(textString){
让关键字=['Text1','Text2','Text3','Text4','Text5'];
让计数=0;
关键字.forEach(函数(关键字){
if(textString.includes(关键字)){
计数++;
}
});
返回计数;
}

log(wordCount('Layer 1,Text1')循环浏览关键字,如果匹配
则增加一个计数器

函数字数(textString){
让关键字=['Text1','Text2','Text3','Text4','Text5'];
让计数=0;
关键字.forEach(函数(关键字){
if(textString.includes(关键字)){
计数++;
}
});
返回计数;
}

log(wordCount('Layer 1,Text1')
您可以使用
Array.reduce()
Array.includes()
来获取数字。由于单词列表可能会更改,因此可以使用部分应用程序创建一个函数,该函数接受单词列表,并返回一个可用于文本的函数:

const wordCount=(单词)=>(层)=>
减少((c,w)=>
层。包括(w)?c+1:c,
0);
const count=wordCount(['Text1','Text2','Text3']);
const result=count('Layer 1,Text1');

console.log(result)//1
您可以使用
Array.reduce()
Array.includes()
来获取数字。由于单词列表可能会更改,因此可以使用部分应用程序创建一个函数,该函数接受单词列表,并返回一个可用于文本的函数:

const wordCount=(单词)=>(层)=>
减少((c,w)=>
层。包括(w)?c+1:c,
0);
const count=wordCount(['Text1','Text2','Text3']);
const result=count('Layer 1,Text1');

console.log(result)//1
为什么不将关键字保存在一个静态数组中,并查询任何给定的字符串以获得匹配计数?这里还有一些工作要做,因为它需要足够聪明来处理标点符号。正则表达式可以很容易地处理这一部分

const getWordCount=(短语、关键字)=>{
如果(短语){
短语=短语.toLowerCase();
常量短语数组=短语分割(“”);
让计数=0;
phraseArray.forEach((word)=>{
if(关键字.find(k=>k.toLowerCase()==word)){
返回计数+=1;
}
});
返回计数;
}
返回0;
};
const str='这是我的句子。有很多人喜欢它,但这一个是我的;
const count=getWordCount(str,['like','are','this']);

控制台日志(计数)为什么不将关键字保存在一个静态数组中,并查询任何给定的字符串以获得匹配计数?这里还有一些工作要做,因为它需要足够聪明来处理标点符号。正则表达式可以很容易地处理这一部分

const getWordCount=(短语、关键字)=>{
如果(短语){
短语=短语.toLowerCase();
常量短语数组=短语分割(“”);
让计数=0;
phraseArray.forEach((word)=>{
if(关键字.find(k=>k.toLowerCase()==word)){
返回计数+=1;
}
});
返回计数;
}
返回0;
};
const str='这是我的句子。有很多人喜欢它,但这一个是我的;
const count=getWordCount(str,['like','are','this']);

控制台日志(计数)为什么不将关键字保存在一个静态数组中,并查询任何给定的字符串以获得匹配计数?第二个代码块毫无意义。它没有对任何东西使用
关键字[i]
,并且在循环的第一次迭代中立即返回。为什么在第一个块中2是正确的结果?它只匹配一个单词。为什么要从3中减去匹配数?区分大小写呢?@mwilson,谢谢你或你的回答。我知道非常基本的JS,所以我甚至不知道将关键字保持为静态和询问是什么意思。我得查一下。为什么不把关键字保存在一个静态数组中,然后查询任何给定的字符串以获得匹配计数呢?你的第二个代码块毫无意义。它没有对任何东西使用
关键字[i]
,并且在循环的第一次迭代中立即返回。为什么在第一个块中2是正确的结果?它只匹配一个单词。为什么要从3中减去匹配数?区分大小写呢?@mwilson,谢谢你或你的回答。我知道非常基本的JS,所以我甚至不知道将关键字保持为静态和询问是什么意思。我必须查一下。非常抱歉,但不幸的是,我使用的是Nashorn,它没有箭头功能,因此在我这边不起作用:-(这是我得到的结果:java.lang.AsertioError:无法为:5生成字节码。是否有方法可以使用支持nashorn的语言重新编写代码?谢谢。非常抱歉,但不幸的是,我使用的是nashorn,它没有箭头函数功能,因此在我这方面不起作用:-(这是我得到的结果:java.lang.AsertioError:Failed gen