I have two arrays:
var arr1 = ["1", "2", "3"],
arr2 = ["2", "5", "8"];
What is the best way to determine if any element from arr1 matches any element in arr2?
I'm using jquery, so for now I'm looping arr1 and calling $.inArray(arr1[i], arr2);
but I'm curious if there's a better way.
Edit
If anyone is curious about my exact case, it's a google map where I'm looping the markers to see if any of the filtered types are found in the types stored with the marker. If any are found, I show the marker. Otherwise it's hidden. [This code works - will test soon with lots of markers]
var filters = $("#map-search :checked").map(function () {
return this.value;
}).get(),
markerIndex = 0;
for (markerIndex; markerIndex < AKTA.Map.markers.length; markerIndex++) {
var marker = AKTA.Map.markers[markerIndex],
isVisible = false;
if (filters.length > 0) {
var filterIndex = 0;
for (filterIndex; filterIndex < filters.length; filterIndex++) {
if ($.inArray(filters[filterIndex], marker.types) != -1) {
isVisible = true;
break;
}
}
}
marker.setVisible(isVisible);
}