【问题标题】:Counting words in javascript and push it into an object在javascript中计算单词并将其推送到对象中
【发布时间】:2016-10-18 07:34:35
【问题描述】:

我想实现一个通过单词计数并返回单词及其出现次数的 javascript 程序,例如 {hello : 2, "@hello":1, world : 1, toString:1}

下面是我的代码,但我只得到总字数

function words(str) { 
    app = {};
    return str.split(" ").length;
}

console.log(words("hello world"));   

【问题讨论】:

标签: javascript count word


【解决方案1】:

使用reduce迭代words数组,并统计实例数:

function words(str) { 
    return str.split(" ").reduce(function(count, word) {
      count[word] = count.hasOwnProperty(word) ? count[word] + 1 : 1;
      
      return count;
    }, {});
}

console.log(words("reserved words like prototype and toString ok? Yes toString is fine"));

【讨论】:

  • 我问了一个关于计数的问题。这看起来可能是我正在寻找的解决方案,但我不确定如何为我拥有的数据实现它,这些数据位于数组中。如果你有时间,请看看。谢谢 :: stackoverflow.com/q/66035684/153923
【解决方案2】:

一种 ES6 方法,可减少诸如字符串之类的项目数组并返回计数:

const strFrequency = function (stringArr) {
  return stringArr.reduce((count, word) => {
        count[word] = (count[word] || 0) + 1;
        return count;
  }, {})
}

let names = ["Bob", "Bill", "Bo", "Ben", "Bob", "Brett", "Ben", "Bill", "Bo", "Ben", "Bob", "Ben"];

console.log(strFrequency(names));
// => {Bob: 3, Bill: 2, Bo: 2, Ben: 4, Brett: 1}

【讨论】:

    【解决方案3】:
    function words(str){
    
    
        var words = [];
    
        //check if words list is empty if so then insert the first word into the array
    
    
        if(!words.length){
            var word = str.split(" ")[0];
            words.push({"word":word, "occurences":1});
        }
    
        //convert string to array so you can iterate through it
        str = str.split(" ");
    
        //iterate through the array starting from the first position because word at the position 0 is already in the array
        for(var i = 1; i<str.length; i++){
    
            //iterate through the words list to the see if the word has appeared yet
            var wordExists = false;
    
            for(var j = 0; j<words.length; j++){
                if(str[i] == words[j].word){
    
                    //word exists in word so count one up
                    words[j].occurences += 1;
    
                    //used to prevent the word from being inserted twice
                    wordExists = true;
                    break;
                }
            }
    
            //insert new word in words if it 
            if(!wordExists){
                words.push({"word":str[i], "occurences":1});
            }
        }
        return words;
    }
    

    【讨论】:

      【解决方案4】:

      这是代码和我通过@ori传递的内容

      function words(str) { 
          var adjustedStr = str.replace(/\n|\t|\s\s+/g, ' ');
          return adjustedStr.split(' ').reduce(function(count, word) {
          count[word] = (count[word] || 0) + 1;
      
          return count;
          }, {});
      }
      
      console.log(words("reserved words like prototype and toString ok?"));
      

      它安慰了

      {toString: "function toString() { [native code] }1"}
      

      【讨论】:

      • 检查 hasOwnProperty,就像接受的答案显示一样。但我也做过一个,你使用 null 构造函数创建一个更普通的对象文字。
      【解决方案5】:

      要在不使用 hasOwnerProperty 的情况下允许对象字面量中的保留字,您可以为 Object 使用 null 构造函数。

      例如。

      function words(str) { 
          var adjustedStr = str.replace(/\n|\t|\s\s+/g, ' ');
          return adjustedStr.split(' ').reduce(function(count, word) {
            count[word] = (count[word] || 0) + 1;
            return count;
          }, Object.create(null));
      }
      
      console.log(words("reserved words like prototype and toString ok?"));

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2015-05-05
        • 2012-12-15
        • 1970-01-01
        • 1970-01-01
        • 2020-02-03
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多