Counting words in javascript and push it into an object
Asked Answered
C

5

7

I want to achieve a javascript program that count through a word and return the word and the number of times it appears eg {hello : 2, "@hello":1, world : 1, toString:1}

below is my code but i only get the total number of words

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

console.log(words("hello world"));   
Collinsworth answered 18/10, 2016 at 7:34 Comment(1)
Possible duplicate of JavaScript Group By ArrayDisunite
R
14

Use reduce to iterate the words array, and count the instances:

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"));
Robespierre answered 18/10, 2016 at 7:37 Comment(1)
I asked a question about counting. This looks like it could be the solution I am looking for, but I am not sure how I would implement it for the data I have, which is in an array. If you have time, please take a look. Thanks :: https://mcmap.net/q/1476278/-filling-highchart-pie-graph-on-kendo-grid-databound-event/153923Hamartia
I
3

An ES6 approach that reduces over an array of items such as strings and returns the count:

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}
Inheritance answered 27/10, 2017 at 9:53 Comment(0)
D
2
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;
}
Danieledaniell answered 18/10, 2016 at 7:54 Comment(0)
C
0

This is the code and what I passed through @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?"));

it consoled out

{toString: "function toString() { [native code] }1"}
Collinsworth answered 18/10, 2016 at 11:5 Comment(1)
Checking the hasOwnProperty, like accepted answer show. But I've also done one were you create a more plain object literal using a null constructor.Dillydally
D
0

To allow reserved words in object literals without using the hasOwnerProperty, you could use a null constructor for the Object.

eg.

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?"));
Dillydally answered 18/10, 2016 at 12:21 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.