compareCandidates = (candidate1, candidate2) => {
const results1 = candidateResultsByCounty(candidate1)
const results2 = candidateResultsByCounty(candidate2)
const totals1 = totalVotesByCounty(results1[0].RaceName, results1[0].year)
const totals2 = totalVotesByCounty(results2[0].RaceName, results2[0].year)
return counties.map(county => {
const candidate1Votes = results1.find(d => d.CountyName === county).votes
const candidate1Total = totals1.find(d => d.county === county).totalVotes
const candidate2Votes = results2.find(d => d.CountyName === county).votes
const candidate2Total = totals2.find(d => d.county === county).totalVotes
return {
county: county,
candidate1Votes: candidate1Votes,
candidate1Percent: candidate1Votes / candidate1Total,
candidate2Votes: candidate2Votes,
candidate2Percent: candidate2Votes / candidate2Total,
difference: candidate2Votes - candidate1Votes,
differencePercent: (candidate2Votes / candidate2Total) - (candidate1Votes / candidate1Total),
}
})
}