Given an array:
var nationList = ["Austria", "France", "Austria", "Spain", "Austria"]
Given that the following gets the current svg path
property (I am using leaflet map, so this value is in the json
).
var countryName = e.target.feature.properties.name;
I run the following which I found on this answer, basically when I mouse hover a polygon, I get countryName and I check how many times is within that array:
var fours = nationList.filter(function(it) {return it === countryName;});
var result = fours.length;
But if I do: console.log(result);
it always gives me 1
or 0
if no match.
In the real scenario an example of console.log(nationList);
gives:
["Afghanistan", "Afghanistan", "Afghanistan", "Afghanistan", "Afghanistan", "Afghanistan", "Afghanistan", "Afghanistan", "Afghanistan", "Afghanistan", "Afghanistan", "Afghanistan", "Afghanistan", "Afghanistan", "Afghanistan", "Afghanistan", "Afghanistan", "Afghanistan", "Afghanistan", "Afghanistan", "Afghanistan", "Afghanistan", "Afghanistan", "Afghanistan", "Afghanistan", "Afghanistan", "Afghanistan", "Afghanistan", "Afghanistan", "Afghanistan", "Afghanistan", "Afghanistan", "Afghanistan", "Afghanistan", "Afghanistan", "Afghanistan", "Afghanistan", "Afghanistan", "Afghanistan", "Afghanistan", "Afghanistan", "Afghanistan", "Afghanistan", "Afghanistan", "Afghanistan", "Afghanistan", "Afghanistan", "Afghanistan", "Afghanistan", "Afghanistan", "Afghanistan", "Afghanistan", "Afghanistan",
UDPATE
What i did before was to try to count each value:
var count = [];
nationList.sort();
nationList.forEach(function(i) {
var i = i.replace(/^\s+/g, "");
count[i] = (count[i]||0) + 1;
});
console.log(count);
That's fine as it gives me for example: Austria: 22
But when I do:
function onEachFeature(feature, layer) {
layer.on({
click: selectNation,
mouseover: printCount
});
}
function printCount(e) {
var countryName = e.target.feature.properties.name;
var filteredNations = nationList.filter( nation => nation ===countryName);
var result = filteredNations.length;
console.log(result);
}
Each time I mouse hover
a path
with a property name
, lets say Austria, even tho within the array
we have many repeating occurrences, the console
gives me 1
if a match is there or 0