-3

I need to loop 2 arrays and see if they have matching objects and if so, add a marker to a map and when i click on it I want to tell the number of total results for matching coordinates which in the following example would be 2. The marker is fine as I am looping the arrays and placing it based on the coordinates but I can't give the total number currencies within the same coordinates.

Therefore I have:

latitude = [{"45.99"},{43.22},{"45.99"}];

longitude = [{"9.22"},{"2.645"},{"9.22"}];

This answer helps a bit, basically it uses filter but what I would like to do is to check if both arrays have any matching values and place the number of results in a modal.

Currently I do:

$("#results .modal-body").html("Risultati"+" " +longitude.length + "<button id='goToResul' type='button' class='btn btn-danger'>Go to result</button>");

But that would check the length of the whole longitude array while in the arrays example we have twice 45.99 and 9.22 which means 2 matches.

What might helps is that both longitudes and latitudes follow the same index

downFast
  • 31
  • 1
  • 6
  • there are no matching lat and long in your example? – ACD Nov 07 '18 at 05:50
  • @ACD why not? we have twice 45.99 and 9.22 which means 1 match – downFast Nov 07 '18 at 05:51
  • And `latitude = [{"45.99"},{43.22},{"45.99"},{43.22}]; longitude = [{"9.22"},{"2.645"},{"9.22"}];` would be? – ACD Nov 07 '18 at 05:54
  • @ACD I don't want to find a latitude or a longitude, I want to find the number of total results for matching coordinates which in this case would be 1 – downFast Nov 07 '18 at 05:55
  • I understand that but on the array I've given where there are 2 pairs of the same value while on longitude only has 1 pair. What would be the expected result? Still 1? – ACD Nov 07 '18 at 05:59
  • I think the way they're looking at the two arrays is latitude[0],longitude[0] matches latitude[2], longitude[2]. Maybe? – chairmanmow Nov 07 '18 at 05:59
  • @chairmanmow yes indeed, but matches latitude[2], longitude[2] should give me 1 as I only need the final number of the matches – downFast Nov 07 '18 at 06:01
  • @ACD it only occurs once that we have 2 matching longitudes and 2 matching latitudes, so the result would be 1 – downFast Nov 07 '18 at 06:05
  • 1
    And in what case do we get 2 as a result? – Nitish Narang Nov 07 '18 at 06:07
  • @NitishNarang there could be plenty of matchings, if we will have 3 longitudes and 3 latitudes matchings then we'll say 3 matches, the matches must be both for same longitude and latitudes or they won't be a complete match – downFast Nov 07 '18 at 06:09
  • @downFast Nitish asks a good question, it's hard to tell what you're doing and if you only give one example there's no way to figure out the pattern you expect just taking guesses – chairmanmow Nov 07 '18 at 06:09
  • @chairmanmow i've replied to Nitish – downFast Nov 07 '18 at 06:10
  • @downFast i know, but you didn't answer his question, you just think you did. What is a 'match'? If there were another 45.99,9.22 pair in those two arrays would there be two matches? Or is that the same match? – chairmanmow Nov 07 '18 at 06:12
  • @chairmanmow it would be count as 3 in that case – downFast Nov 07 '18 at 06:14
  • So if the previous result was 1, and I add one more pair the number of matches becomes 3? 1+1 = 3? – chairmanmow Nov 07 '18 at 06:15
  • @chairmanmow sorry I was mistaking by saying 1 match, there are 2 matches not 1. – downFast Nov 07 '18 at 06:15
  • I got it :) That's why I asked. err pointed it out – chairmanmow Nov 07 '18 at 06:16
  • @NitishNarang sorry, I meant we currently have 2 matches not 1 – downFast Nov 07 '18 at 06:17
  • And let's say I added two more matching elements to each of those array that are new, the number of matches should go up by one, since they represent one new match? – chairmanmow Nov 07 '18 at 06:19
  • @chairmanmow think of this, we have some blog posts with some coordinates. I loop them and I place markers on the map, when I click on a marker tho, it should tell me if there are 2 or more blog posts attached to those coordinates. So since I am saving long and lat, I need to find the matches if I want to show "There are 3 posts with these coordinates" – downFast Nov 07 '18 at 06:22
  • Ok good explanation, I think someone will come up with an answer based off that, I'm not going to write one this second but it's pretty clear what your question is. I can answer it but I'm lazy so someone go for the glory – chairmanmow Nov 07 '18 at 06:24

2 Answers2

1

As per my understanding, you can try below approach

var lat = ["45.99","43.22","45.99", "45.99"]
  , lng = ["9.22","2.645","9.22", "9.22"]
  , latlngCount = Object.entries(lat.reduce((o, d, i) => (o[lat[i]+'-'+lng[i]] = (o[lat[i]+'-'+lng[i]] || 0) + 1, o) , {})).filter(([latlng, count]) => count > 1)
  , matchingCount = latlngCount.reduce((sum, [latlng, count]) => sum + count, 0)

console.log('Matching lat lng', latlngCount)
console.log('Matching Count', matchingCount)
Nitish Narang
  • 4,124
  • 2
  • 15
  • 22
0

Do you mean something like this?

count=0
latitude = ["45.99","43.22","45.99"];
longitude = ["9.22","2.645","9.22"];
lat=latitude.concat([])
long=longitude.concat([])
for(i in latitude){
  lat.shift()
  long.shift()
  if((index=lat.indexOf(latitude[i]))!==-1){
    if(long[index]==longitude[i]){
      //DO SOMETHING IF A PAIR IS FOUND
      count++
    }
  }
}
console.log(count)
FZs
  • 16,581
  • 13
  • 41
  • 50