Skip to content

Instantly share code, notes, and snippets.

@itsjoekent
Created May 19, 2016 23:31
Show Gist options
  • Save itsjoekent/bded605a645b9ab6d44c68881dc2ef80 to your computer and use it in GitHub Desktop.
Save itsjoekent/bded605a645b9ab6d44c68881dc2ef80 to your computer and use it in GitHub Desktop.
Generates a CSV to represent electoral college
var increment = 100000;
// http://halistechnology.com/2015/05/28/use-javascript-to-export-your-data-as-csv/
function convertArrayOfObjectsToCSV(args) {
var result, ctr, keys, columnDelimiter, lineDelimiter, data;
data = args.data || null;
if (data == null || !data.length) {
return null;
}
columnDelimiter = args.columnDelimiter || ',';
lineDelimiter = args.lineDelimiter || '\n';
keys = Object.keys(data[0]);
result = '';
result += keys.join(columnDelimiter);
result += lineDelimiter;
data.forEach(function(item) {
ctr = 0;
keys.forEach(function(key) {
if (ctr > 0) result += columnDelimiter;
result += item[key];
ctr++;
});
result += lineDelimiter;
});
return result;
}
function getData(state, prefix, totalElectorals, votingAgePop) {
var data = [];
var pplPerVote = votingAgePop / totalElectorals;
var intervals = pplPerVote / increment;
for (var es = 0; es < totalElectorals; es++) {
for (var i = 0; i < intervals; i++) {
data.push({
"State": state,
"Electoral Vote": `${prefix} - Electoral Vote ${es}`,
"Interval": `100K ${prefix} Voters (${i})`
});
}
}
// console.log(pplPerVote, intervals);
return data;
}
var ny = getData("New York", 'ny', 31, 19594330);
var wy = getData("Wyoming", 'wy', 3, 575250);
console.log(convertArrayOfObjectsToCSV({
data: ny.concat(wy)
}));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment