-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

21
  • there are no matching lat and long in your example? Commented Nov 7, 2018 at 5:50
  • @ACD why not? we have twice 45.99 and 9.22 which means 1 match Commented Nov 7, 2018 at 5:51
  • And latitude = [{"45.99"},{43.22},{"45.99"},{43.22}]; longitude = [{"9.22"},{"2.645"},{"9.22"}]; would be? Commented Nov 7, 2018 at 5: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 Commented Nov 7, 2018 at 5:55
  • 1
    And in what case do we get 2 as a result? Commented Nov 7, 2018 at 6:07

2 Answers 2

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)

Sign up to request clarification or add additional context in comments.

Comments

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)

2 Comments

what would it be the result of this?
@downFast this have a section (marked by the comment in code) what you need to replace with something, what you want to do for each pairs

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.