计算javascript中字符串中的元音

时间:2019-01-20 20:48:00

标签: javascript ecmascript-6

我想使用javascript的reduce()方法获取字符串中元音的计数。下面是代码,问题是解构acc后a,e,i,o,u的值未定义。

const str = 'I am just a string. I mean nothing serious and I am being used to count the number of vowels I have.';

const strArr = str.split('');



const mapVowels = strArr.reduce(countVowels, {});

function countVowels(acc, char) {
  console.log(char)
  var { a = 0, e = 0, i = 0, o = 0, u = 0 } = acc;
  if (char === "a") {
    return {...acc, a: a + 1};
  }
  if (char === 'i') {
    return { ...acc, i: i + 1}
  }
  if (char === 'e') {
    return { ...acc, e: e + 1}
  }
  if (char === 'o') {
    return { ...acc, o: o + 1}
  }
  if (char === 'u') {
    return { ...acc, u: u + 1}
  }
}
console.log(mapVowels)

我希望mapVowels成为具有键a,e,i,o,u的对象,并重视它们在str中重复的次数。

4 个答案:

答案 0 :(得分:5)

当找到非元音字符时,您不会返回acc。因此,acc在下一次迭代中是undefined,并且解构失败。返回acc,即使它不是元音也是如此:

const str = 'I am just a string. I mean nothing serious and I am being used to count the number of vowels I have.';

const strArr = str.split('');

const mapVowels = strArr.reduce(countVowels, {});

function countVowels(acc, char) {
  var { a = 0, e = 0, i = 0, o = 0, u = 0 } = acc;
  
  if (char === "a") {
    return {...acc, a: a + 1};
  }
  if (char === 'i') {
    return { ...acc, i: i + 1}
  }
  if (char === 'e') {
    return { ...acc, e: e + 1}
  }
  if (char === 'o') {
    return { ...acc, o: o + 1}
  }
  if (char === 'u') {
    return { ...acc, u: u + 1}
  }
  
  return acc;
}
console.log(mapVowels)

此外,您可以通过创建数组或元音串并使用DRYString.includes()来识别元音来使代码更Array.inclues()

const vowels = 'aieou';

const str = 'I am just a string. I mean nothing serious and I am being used to count the number of vowels I have.';

const strArr = str.split('');

const mapVowels = strArr.reduce(countVowels, {});

function countVowels(acc, char) {
  if(!vowels.includes(char)) return acc;
  
  const { [char]: val = 0 } = acc;
  
  return {...acc, [char]: val + 1};
}

console.log(mapVowels)

答案 1 :(得分:2)

Ori向您介绍了代码不起作用的原因。您还会重复很多代码,因此,这里有一个reduce示例,可让您在一行中建立计数。

const str = 'I am just a string. I mean nothing serious and I am being used to count the number of vowels I have.';
const vowelRe = /[aeiou]/;

const countVowels = str.split('').reduce((acc, c) => {

  // Test if the current letter is a vowel
  const found = vowelRe.test(c);
 
  // If it is: if the vowel exists in the object as a key increase the count,
  // and if it doesn't set it to zero, and then increase the count
  if (found) acc[c] = (acc[c] || 0) + 1;

  // return the accumulator
  return acc;
}, {});

console.log(countVowels);

答案 2 :(得分:0)

您可以执行以下操作:

const str = 'I am just a string. I mean nothing serious and I am being used to count the number of vowels I have.';

const VOWELS = ['a', 'e', 'i', 'o', 'u', 'y'];

var count = str.trim().split('').reduce((accum, el) => {
  if (VOWELS.indexOf(el) > -1) {
    accum[el] += 1;
  }
  return accum;
}, {
  'a': 0,
  'e': 0,
  'i': 0,
  'o': 0,
  'u': 0,
  'y': 0
});

console.log(count);

答案 3 :(得分:0)

这种方法采用所需字符,构建对象进行计数,然后检查并计数字符(如果在对象中)。

const
    vowels = 'aieou';
    str = 'I am just a string. I mean nothing serious and I am being used to count the number of vowels I have.';
    mapVowels = Array
        .from(str.toLowerCase())
        .reduce(
            (acc, char) => (char in acc && acc[char]++, acc),
            Object.assign(...Array.from(vowels, c => ({ [c]: 0 })))
        );

console.log(mapVowels);