Javascript: Counting frequency of emojis in text

Viewed 1494

I'm trying to count the frequency of emojis in a block of text. For example:

"I love  so much  " -> [{:3}, {:1}]

In order to count the frequency of characters in a block of text, I'm using

function getFrequency(string) {
    var freq = {};
    for (var i=0; i<string.length;i++) {
        var character = string.charAt(i);
        if (freq[character]) {
           freq[character]++;
        } else {
           freq[character] = 1;
        }
    }

    return freq;
};

source: https://stackoverflow.com/a/18619975/4975358

^The above code works great, but it does not recognize emoji characters:

{�: 1,  �: 3,  �: 2}

Also, I'd prefer the output to be a list of json objects of length 1, as opposed to one long json object.

2 Answers
Related