I have the following code which creates objects of each word (words that are in wordsArray) and checks for the number of occurrences of each word and stores this along with each object.
var wordOccurrences = { };
for (var i = 0, j = wordsArray.length; i < j; i++) {
wordOccurrences[wordsArray[i]] = (wordOccurrences[wordsArray[i]] || 0) + 1;
}
console.log(wordOccurrences);
This outputs:
Object { hello: 1, world!: 2, click: 1, here: 1, goodbye: 1 }
I would appreciate if someone could help me with ordering the objects by their occurrences e.g. ascending or descending order. I have tried a few ways but none has yielded the correct results.
The desired output would be something like:
world! 2
hello 1
click 1
here 1
goodbye 1