-2

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

rob.m
  • 9,843
  • 19
  • 73
  • 162
  • you should compare the two using the same case. it is possible that one has a different casing (`toUpperCase()` or `toLowerCase()`). also, make sure that full names are being used and not one full and one with the abbreviation. – Get Off My Lawn Jun 27 '18 at 19:26
  • @GetOffMyLawn it's all fine on that side – rob.m Jun 27 '18 at 19:26
  • 2
    No, it is not. The code youve shown is working – Jonas Wilms Jun 27 '18 at 19:27
  • @JonasW. the case sensitves is fine, if you're saying the code is working, then at the very least should be somewhere else but the cases are fine. – rob.m Jun 27 '18 at 19:30
  • 1
    @rob.m could you at least give us a snippet or fiddle showing exactly how this doesn't work for you, because as you can see from this, it does: http://jsfiddle.net/guywnkpL/ – Rory McCrossan Jun 27 '18 at 19:30
  • @RoryMcCrossan will try to put one up, thanks – rob.m Jun 27 '18 at 19:31
  • you could also try to use `e.currentTarget` which is the target that the event is attached to, as opposed to `e.target` which identifies the element on which the event occurred. – Get Off My Lawn Jun 27 '18 at 19:37
  • @GetOffMyLawn I've updated my question if that helps – rob.m Jun 27 '18 at 19:43
  • @RoryMcCrossan it's a bit complecate to reproduce it, check if the question now updated is of any help – rob.m Jun 27 '18 at 19:43
  • Have you tried to `console.log(countryName)`? – Rory McCrossan Jun 27 '18 at 19:45
  • @RoryMcCrossan yup, that works, gives the correct name – rob.m Jun 27 '18 at 19:46
  • In that case there's not much help anyone can offer as there's no obvious issues in what you've shown. There must be another underlying problem in your logic. – Rory McCrossan Jun 27 '18 at 19:47
  • resolved, and it was all my fault as I had `$.unique(nationList.sort());` which means it was removing any duplicates... – rob.m Jun 27 '18 at 20:07

1 Answers1

1

The code that you have posted seems to be working correctly, but as GetOffMyLawn stated you should compare the two as the same case just to be sure.

var nationList = ["Austria", "France", "Austria", "Spain", "Austria"];
var countryName = e.target.feature.properties.name;
var fours = nationList.filter(
  function(it) {
    return it.toLowerCase() === countryName.toLowerCase();
  });
var result = fours.length;

console.log(result);

Sorry for the short answer, but not enough reputation at the moment to just post comments.

DCCoder
  • 1,587
  • 4
  • 16
  • 29
  • Thanks but as I replied to GetOffMyLawn, the cases are fine. matter of fact applying toLowerCase doesn't solve the issue – rob.m Jun 27 '18 at 19:36
  • Not saying that anything is wrong with the cases, however it is simply best to prepare for the unexpected. Last thing anyone wants is their code not working one day 5 years from now and no clue why, simple things like this prevent that. Unless you specifically want say "georgia" and "Georgia" to be different. – DCCoder Jun 27 '18 at 19:41
  • Yes, that actually helps a lot more. It seems as though you may be referencing the countryName wrong, although I'm not sure. The code in printCount works, so my only thought is that it must be this line: e.target.feature.properties.name; – DCCoder Jun 27 '18 at 19:48
  • Thought that too, but if I do `console.log(countryName);` works fine. – rob.m Jun 27 '18 at 19:53
  • Could there be leading or trailing spaces? Maybe try trimming countryName before your pass it to your filter? – DCCoder Jun 27 '18 at 20:21
  • it was far more lame than that, the code is huge and I had `$.unique(nationList.sort())` somewhere else and missed it. Resolved now. Will accept your answer tho – rob.m Jun 27 '18 at 20:47
  • 1
    Oh ok, glad you were able to locate the problem. – DCCoder Jun 27 '18 at 20:47