简体   繁体   English

如何检查字符串或数组中单词的多次出现

[英]How to check for multiple occurrences of word in string or array

I need to get the count of a specified set duplicate words in a string. 我需要获取字符串中指定集合重复单词的计数。

I can return the occurrences of one word but I am stuck at multiple words. 我可以返回一个单词的出现,但我卡在多个单词中。

const sData = 'am=1&am=2&pm=1&pm=2&pm=3&ac=1&dc=1';
const aData = sData.split('&');

const nData = aData.filter(el => el.includes('am')).length;

This returns 2 which is expected but I need to do the same for pm , ac and dc and as I understand includes does not accept multiple values. 这将返回2 ,这是预期的,但我需要对pmacdc进行相同的操作,并且据我所知, includes不接受多个值。

You can encompass repeated work using functions ! 您可以使用功能进行重复工作!

const countOccurrencesOf = (word, search) => {
  return word.filter(el => el.includes(search)).length;
};

Now, you can re-use this function for all of your other string finding needs: 现在,您可以重新使用此函数来满足其他所有查找字符串的需求:

countOccurrencesOf(aData, 'pm');
countOccurrencesOf(aData, 'ac');
countOccurrencesOf(aData, 'dc');

What you're looking for, is a recursive function. 您正在寻找的是递归函数。 Within the example you have provided, I'm assuming your parsing URL Query variables. 在您提供的示例中,我假设您正在解析URL查询变量。 So, let's pretend that the following are your delimiters: & or =. 因此,让我们假设以下是您的分隔符:&或=。

This function shows a general idea of what it seems like you're trying to acheive. 此功能显示了您想要达到的效果的总体思路。

 function containsDuplicates(string,delimiter,foundDuplicates){
    var str = string.split(delimiter);

    if(foundDuplicates.includes(str)){
        return true;
    }else if(string.indexOf(str) < string.length){
        foundDuplicates.push(str);
        return containsDuplicates(string,delimiter,foundDuplicates);
    }
    else{
        return false;
    }
}

Try using map instead: 尝试使用地图代替:

const sData = 'am=1&am=2&pm=1&pm=2&pm=3&ac=1&dc=1';
const aData = sData.split('&');

// define the stings you want to count
const nData = {am: 0, pm: 0, ac: 0}

// iterate over the split input data
const val = aData.map((obj) => {
    // iterate over the nData
    Object.keys(nData).forEach((count) => {
        // if the input contains the count, add to the count
        if (obj.includes(count)) nData[count]++;
    })
})
console.log(nData); // returns: { am: 2, pm: 3, ac: 1 }

Reduce would probably work as well! 减少可能也会起作用!

  • Create an array of keywords you want to find. 创建要查找的关键字数组。
  • Create a regex by separating the words with a | 通过用|分隔单词来创建正则表达式 .
  • Use match get all the matches for the keywords specified. 使用match获取指定关键字的所有匹配项。
  • Create a counter object. 创建一个counter对象。 Loop through the array of matches and count each occurrence 遍历匹配数组并计算每次出现的次数

 const str = 'am=1&am=2&pm=1&pm=2&pm=3&ac=1&dc=1', toFind = ['am', 'pm', 'ac'], regex = new RegExp(toFind.join("|"), "g"), matches = str.match(regex); console.log(matches) const counter = {}; matches.forEach(m => counter[m] = counter[m] + 1 || 1) console.log(counter) 
 .as-console-wrapper { max-height: 100% !important; top: 0; } 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何检查给定字符串中数组中的单词是否存在 - How to check if word in array exists in a given string Javascript / jQuery检查字符串是否包含一个单词或是否出现可选单词之一 - Javascript/jQuery check if string contains a word or occurrences of one of the optional words 在字符串中查找单词出现次数 - Finding word occurrences in a string 检查字符串中是否有任何单词在数组中 - Check if any word in string is in an array 如何检查输入的单词是否等于数组中的任何字符串 - How to check if typed word is equal to any string from array 如何拆分camelCase字符串并检查每个拆分字是否是数组的一部分? - How to split a camelCase string and check if each split word is part of an array or not? 没有正则表达式,如何检查数组中的单词是否包含在字符串中? - How to check whether word within array is included in string, without regex? 如何替换JavaScript中多次出现的字符串? - How to replace multiple occurrences of string in javascript? 如何用javascript中数组的元素替换字符串的出现? - How to replace occurrences of a string by elements of an array in javascript? 如何计算单词在字符串中的出现次数/将其存储在对象中? - How do I count the number of occurrences of a word in a string/store it in an object?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM