Skip to content

Instantly share code, notes, and snippets.

@niftydigits
Created February 11, 2012 22:27
Show Gist options
  • Save niftydigits/1804714 to your computer and use it in GitHub Desktop.
Save niftydigits/1804714 to your computer and use it in GitHub Desktop.
CouchDB Map / Reduce for Answers to Straw Poll Questions
function(doc) {
emit(doc.constituency, doc);
}
function(key, values){
var total = 0;
var voting = 0;
var votes = {
libdems: 0,
conservatives: 0,
labour: 0,
green: 0,
ukip: 0,
bnp: 0,
other: 0
}
for(var i=0; i < values.length; i++){
total++;
if(values[i].voting==1){
var party = values[i].party.toLowerCase();
party = party.replace(/ /g,"");
votes[party]++;
}
}
return {total: total, parties: votes};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment