Created
January 27, 2019 11:31
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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