Created
April 16, 2015 03:56
-
-
Save jonathan-ostrander/aa4ee168f019b6e916a5 to your computer and use it in GitHub Desktop.
Script to run on an exported Untappd JSON file that is open in Chrome to get your average brewery ratings.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Array.prototype.average = function () { | |
var sum = 0, j = 0; | |
for (var i = 0; i < this.length, isFinite(this[i]); i++) { | |
sum += parseFloat(this[i]); ++j; | |
} | |
return j ? sum / j : 0; | |
}; | |
var loadJSON = function() { | |
return JSON.parse(document.getElementsByTagName("pre")[0].innerHTML); | |
} | |
var getAverages = function() { | |
var checkins = loadJSON(); | |
var breweries = {}; | |
checkins.forEach(function (d) { | |
var brewery = d.brewery_name; | |
var rating = Number(d.rating_score); | |
if (breweries[brewery]) { | |
breweries[brewery].ratings.push(rating); | |
} else { | |
breweries[brewery] = {ratings: [rating]}; | |
} | |
}); | |
for (var b in breweries) { | |
var avg = breweries[b].ratings.average(); | |
breweries[b].average = avg; | |
console.log(b + ": " + avg); | |
} | |
} | |
getAverages(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment