Skip to content

Commit 3c302bb

Browse files
committed
races api, fixed empty array return, funky solution
1 parent f15fb6c commit 3c302bb

File tree

1 file changed

+30
-13
lines changed

1 file changed

+30
-13
lines changed

race-feed-json/dataLayer.js

Lines changed: 30 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,14 @@ const apiRaces = (variable, body) => {
1414
});
1515
};
1616
const apiCandidates = (variable, body) => {
17-
var races = variable ? body.races.map(x => x.candidates.filter(y => y.candidate_code == parseInt(variable))) : body.races;
18-
return races.reduce((acc, val) => acc.concat(val.candidates.map(c => {
17+
var races = variable ? body.races.map(x => {
18+
if (x.candidates.filter(y => y.candidate_code == parseInt(variable))){
19+
return x;
20+
}
21+
22+
23+
}) : body.races;
24+
return races.reduce((acc, val) => acc.concat(val.candidates.filter(y => variable ? y.candidate_code == parseInt(variable) : y.candidate_code).map(c => {
1925
return {
2026

2127
candidate_code: c.candidate_code,
@@ -27,17 +33,28 @@ const apiCandidates = (variable, body) => {
2733
})), []);
2834
};
2935
const apiCounties = (variable, body) => {
30-
var races = variable ? body.races.map(x => x.counties.filter(y => y.county_name === variable)) : body.races;
31-
return races.reduce((acc, val) => acc.concat(val.counties.map(c => {
32-
return {
33-
county_code: c.county_code,
34-
party_stratum_name: c.party_stratum_name,
35-
registered: c.registered,
36-
total_vote: c.total_vote,
37-
fips_code: c.fips_code,
38-
race_id: val.race_id
39-
};
40-
})), []);
36+
var races = variable ? body.races.map(x => {
37+
if (x.counties.filter(y => y.county_name === variable)){
38+
return x;
39+
}
40+
41+
}) : body.races;
42+
43+
console.log(races);
44+
return races.reduce((acc, val) =>
45+
acc.concat(val.counties.filter(x => variable ? x.county_name === variable : x.county_name).map(c =>
46+
47+
48+
{
49+
return {
50+
county_code: c.county_code,
51+
party_stratum_name: c.party_stratum_name,
52+
registered: c.registered,
53+
total_vote: c.total_vote,
54+
fips_code: c.fips_code,
55+
race_id: val.race_id
56+
};
57+
})), []);
4158

4259
};
4360
module.exports = {

0 commit comments

Comments
 (0)