Skip to content

Instantly share code, notes, and snippets.

@ThomasOrlita
Created January 27, 2019 11:31
Show Gist options
  • Save ThomasOrlita/63862d37b9032d0c878afb455e0019fc to your computer and use it in GitHub Desktop.
Save ThomasOrlita/63862d37b9032d0c878afb455e0019fc to your computer and use it in GitHub Desktop.
Counts the number of identical messenger messages and sorts them from the most common.
var messages = [
{
"sender_name": "Lorem Ipsum",
"timestamp_ms": 0000000000000,
"content": "Message content",
"type": "Generic"
},
.........
];
var counts = [];
messages.forEach(function(x) {
let msg = x['content'];
let index = counts.findIndex(x => x.msg === msg);
if (index !== -1) {
counts[index].count = counts[index].count + 1;
} else {
counts.push({
'msg': msg,
'count': 1
});
index = counts.findIndex(x => x.msg === msg);
}
counts[index][x['sender_name']] = (counts[index][x['sender_name']] || 0) + 1;
});
counts.sort((a, b) => b.count - a.count);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment