JavaScript Count字符不区分大小写

给我们一个字符串,要求我们编写一个函数,该函数返回数组中每个字符的频率。而且我们不应该考虑字符的情况。

为此,最好的方法是遍历字符串,并准备一个以键为字符,频率为值的对象。

这样做的代码将是-

示例

const string = 'ASASSSASAsaasaBBBASvcdNNSASASxxzccxcv';
const countFrequency = str => {
   const frequency = {};
   for(char of str.toLowerCase()){
      if(!frequency[char]){
         frequency[char] = 1;
      }else{
         frequency[char]++;
      };
   };
   return frequency;
};
console.log(countFrequency(string));

输出结果

上面的代码在控制台中的输出将是-

{ a: 10, s: 11, b: 3, v: 2, c: 4, d: 1, n: 2, x: 3, z: 1 }