简体   繁体   English

Javascript分配和增加Obj的键值(使用Reduce)

[英]Javascript assign and increment Obj's key value(with Reduce)

Im following a course online and one of the challenges is this:我正在学习在线课程,其中一个挑战是:

Write a function called vowelCount that accepts a string and returns an object with each key being the vowel and the value being the number of times the vowel occurs in the string (the order of keys in the object does not matter).编写一个名为元音计数的函数,它接受一个字符串并返回一个对象,每个键是元音,值是元音在字符串中出现的次数(对象中键的顺序无关紧要)。

vowelCount('incredible');
// {i:2, e: 2}
vowelCount('awesome');
// {a:1, e:2, o:1}

So far, I've come up with the following code, using Javascript's reduce:到目前为止,我已经想出了以下代码,使用 Javascript 的 reduce:

function vowelCount(word) {
  var vowels = ['a', 'e', 'i', 'o', 'u'];

  var final = word.split('').reduce(function(obj, val, index) {
    if (vowels.indexOf(val) > -1) {
      //obj[val] = 0
      obj[val]++;
    }
    return obj
  }, {})
  console.log(final)
}

I think I'm close but I'm having trouble wrapping my head on how to assign and increment the numerical value of the vowel key at point of checking if its a vowel.我想我已经接近了,但是在检查元音键是否为元音时,我无法确定如何分配和增加元音键的数值。 I tried instantiating the value to 0, but that only keeps the value at 1.我尝试将该值实例化为 0,但这只会将该值保持为 1。

Use short circuit evaluation to check if the value exists, and if not use 0 instead:使用短路评估来检查该值是否存在,如果不存在,则使用 0 代替:

 console.log(vowelCount('incredible')); // {i:2, e: 2} console.log(vowelCount('awesome')); // {a:1, e:2, o:1} function vowelCount(word) { var vowels = ['a', 'e', 'i', 'o', 'u']; return word.split('').reduce(function(obj, val, index) { if (vowels.indexOf(val) > -1) { obj[val] = (obj[val] || 0) + 1; } return obj; }, {}); }

In addition, instead of using an array of vowels and the Array.indexOf() check, you can initialize the result object with the vowels, and increment them directly:此外,您可以使用元音初始化结果对象,并直接增加它们,而不是使用元音数组和Array.indexOf()检查:

 console.log(vowelCount('incredible')); // {i:2, e: 2} console.log(vowelCount('awesome')); // {a:1, e:2, o:1} function vowelCount(word) { return word.split('').reduce(function(obj, val) { if(val in obj) { obj[val]++; } return obj; }, { a: 0, e: 0, i: 0, o: 0, u: 0 }); }

I think this is actually a bad usecase for reduce.我认为这实际上是 reduce 的一个糟糕用例。 Might just use a regular for loop and use the or operator ( || ) to replace undefined with 0 :可能只使用常规 for 循环并使用 or 运算符( || )将undefined替换为0

 function vowelCount(word){
   const vowels = "aeiou";
   const result = {};
   for(const char of word)
     if(vowels.includes(char))
       result[char] = (result[char] || 0) + 1;
   return result;
}

If that is too spooky you could just check if char already exists in result and set it otherwise:如果这太诡异了,你可以检查结果中是否已经存在 char 并设置它:

function vowelCount(word){
   const vowels = "aeiou";
   const result = {};
   for(const char of word){
     if(vowels.includes(char)){
       if(!result[char]) result[char] = 0;
       result[char]++;
     }
  }
   return result;
}

You were very close!你非常接近! It was because you were trying to increment undefined.那是因为你试图增加 undefined。 You must set that value equal to a number on the accumulator before you increment it.在递增之前,您必须将该值设置为等于累加器上的一个数字。

function vowelCount(word) {
    var vowels = ['a', 'e', 'i', 'o', 'u'];

    var final = word.split('').reduce(function(obj, val, index) {
    if (vowels.indexOf(val) > -1) {
        // if we have seen the vowel, we increment it. Otherwise, it is the first time.
        obj[val] ? obj[val]++ : obj[val] = 1;
    }
    return obj
    }, {})
    console.log(final)
}

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM