Search code examples
javascriptarraysjavascript-objectsemoji

Reduce array of emoji objects to get count of each emoji javascript


I have a javascript array as follows:

[
    {moodSymbol: "๐Ÿ˜•"},
    {moodSymbol: "๐Ÿ™‚"},
    {moodSymbol: "๐Ÿค“"},
    {moodSymbol: "๐Ÿ˜"},
    {moodSymbol: "๐Ÿ˜"},
    {moodSymbol: "๐Ÿคช"},
    {moodSymbol: "๐Ÿ˜"},
    {moodSymbol: "๐Ÿคจ"},
    {moodSymbol: "๐Ÿค“"},
    {moodSymbol: "๐Ÿฅฐ"},
    {moodSymbol: "๐Ÿ™‚"},
    {moodSymbol: "๐Ÿ˜ข"},
]

How can I get the count of each emoji and still keep the moodSymbol key name as such:

const data = [
{
    moodSymbol: "๐Ÿ˜",
    count: 3
},
{
    moodSymbol: "๐Ÿ˜•",
    count: 2
}, 
...
]

I also want the top 5 with the highest count. I tried using reduce and I honestly have been having so much trouble getting this to work with emojis.


Solution

  • You can create an object (hash map) to count each symbol and then generate the results array.

    To get the top 5, just sort the result in decending order and then splice the array to have only 5 elements.

    let arr = [ {moodSymbol: "๐Ÿ˜•"}, {moodSymbol: "๐Ÿ™‚"}, {moodSymbol: "๐Ÿค“"}, {moodSymbol: "๐Ÿ˜"}, {moodSymbol: "๐Ÿ˜"}, {moodSymbol: "๐Ÿคช"}, {moodSymbol: "๐Ÿ˜"}, {moodSymbol: "๐Ÿคจ"}, {moodSymbol: "๐Ÿค“"}, {moodSymbol: "๐Ÿฅฐ"}, {moodSymbol: "๐Ÿ™‚"}, {moodSymbol: "๐Ÿ˜ข"}];
    
    let obj = {};
    
    arr.forEach(e => {
        obj[e.moodSymbol] = obj[e.moodSymbol] || 0;
        obj[e.moodSymbol]++;
    });
    
    let res = Object.entries(obj).map(e => ({moodSymbol: e[0], count: e[1]}));
    
    res.sort((a, b) => b.count - a.count).splice(5);
    
    console.log(res);