0

Initially I was trying to count the number of occurrences of each word in a text, for which I found this super useful thread:

word frequency in javascript

Once that I have the result in an object:

Object {I: 1, am: 1, the: 2, big: 2, bull: 1}

  1. how can order the results/object by the number of occurrences? I have tried any type of sort and it didn't work.

  2. how can I know the length of the object? I have tried object.length and any related idea and I always get zero as a result.

Thanks

Community
  • 1
  • 1
  • 1
    Using something like lodash, you could _.pairs the object, then _.sort them by the value. You can't really order an object, and you can't get a length of an object - Object.keys(object).length is as close as it'll get. – Seiyria Feb 07 '17 at 22:27
  • Object properties are unordered. They're nice as a lookup structure, but if you want to sort them use an array of word-count results. – Bergi Feb 07 '17 at 22:38

0 Answers0