If I have an object like this how can I sort using the underscore module in node.js so the values are highest to lowest so that...
{ ZZX: 1, FRA: 5, GBR: 2, USA: 3 }
..becomes
{ FRA: 5, USA: 3, GBR: 2, ZZX: 1 }
If I have an object like this how can I sort using the underscore module in node.js so the values are highest to lowest so that...
{ ZZX: 1, FRA: 5, GBR: 2, USA: 3 }
..becomes
{ FRA: 5, USA: 3, GBR: 2, ZZX: 1 }
You can do
sortedKeys = _.sortBy(Object.keys(obj), function(key){ return obj[key]; }).reverse();
newHash = {};
sortedKeys.forEach (e => (newHash[e] = obj[e]));
console.log(newHash)