I have used the JavaScript .forEach method to iterate through a given array of characters to return an object that indicates the character and the number of times the character occurs:
var textArray = [a, b, b, c, d, e, e, e, f];
var count = {};
textArray.forEach(function(elem) {
count[elem] = count[elem] + 1 || 1;
});
console.log(count);
Would log:
{
a: 1,
b: 2,
c: 1,
d: 1,
e: 3,
f: 1
}
How can I then sort the object by descending occurrence? i.e.:
{
e: 3,
b: 2,
a: 1,
c: 1,
d: 1,
f: 1
}