Skip to content

Instantly share code, notes, and snippets.

@g5codyswartz
Last active April 15, 2016 21:29
Show Gist options
  • Save g5codyswartz/3a39448d5919588fb5627ca0344fd4da to your computer and use it in GitHub Desktop.
Save g5codyswartz/3a39448d5919588fb5627ca0344fd4da to your computer and use it in GitHub Desktop.
// https://gist.github.com/g5codyswartz/3a39448d5919588fb5627ca0344fd4da
var authors = {};
var author;
$(".author_cell").each(function(i,e) {
author = $(e).text().trim();
if (!authors.hasOwnProperty(author)) authors[author] = 0;
authors[author]++
});
// http://jsfiddle.net/lalatino/mcuzr/
function sortObject(obj) {
var arr = [];
var prop;
for (prop in obj) {
if (obj.hasOwnProperty(prop)) {
arr.push({
'key': prop,
'value': obj[prop]
});
}
}
arr.sort(function(a, b) {
return b.value - a.value;
});
return arr; // returns array
}
authors = sortObject(authors);
function keyValueizer(obj) {
var newObj = {};
obj.forEach(function(k,v) {
newObj[k['key']] = k['value'];
});
return newObj;
}
authors = keyValueizer(authors);
console.log(JSON.stringify(authors));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment