So say I have this array:
[
{
source:"A",
country:"United States",
region:"Illinois",
city:"Chicago"
},
{
source:"B",
country:"United States",
region:"Illinois",
city:"Rock Falls"
},
{
source:"C",
country:"United States",
region:"Illinois",
city:"Sterling"
},
{
source:"D",
country:"United States",
region:"Illinois",
city:"Rock Falls"
},
{
source:"E",
country:"United States",
region:"Illinois",
city:"Rock Falls"
}
]
I'm trying to "crowd source" some ip to location data, and I have five sources that each return an object with country, region, city properties, and of course source being that particular source.
What would be the best way to choose the ideal result using underscore.js under these conditions:
If there is a (city, region) pair that occurs more times than any other pair, select that pair (and the country attribute).
If there is no pair that occurs more than another pair, example: (a,a,b,b,c), (a,b,c,d,e) then choose source E and its information.
If source E is not present and there are no pairs in common, choose a random source.
If there is only one source, obviously choose it.
It's also possible for some sources to not be present.