Last active
December 6, 2018 20:07
-
-
Save marinalohova/0bf05af9b20f7b585c1758c6386227ed to your computer and use it in GitHub Desktop.
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
//precalc all distances to major research cities after import and store to respondents database/elastic | |
function precalcDistance(respondents, majorCities) { | |
return respondents.map((respondent) => { | |
let distances = majorCities.reduce((memo, {location}) => { | |
let distance = haversine(location.location, respondent); | |
memo[location.city] = distance; | |
return memo; | |
}, {}); | |
return { ...respondent, distances }; | |
}) | |
} | |
//assuming respondents have the precalculated 'distances' property, we can run search over and over without unnecessary recalulcating. | |
function byDistance(respondents, researchCities, range) { | |
return respondents | |
.filter(respondent => researchCities.some(city => range > respondent.distances[city.location.city])) | |
.sort((a, b) => a.firstName.toLowerCase() > b.firstName.toLowerCase()); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment