1

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);

}
ScottE
  • 21,530
  • 18
  • 94
  • 131
  • Out of completeness: [inArray](https://github.com/jquery/jquery/blob/1.5.1/src/core.js#L663) in core.js at 1.5.1. – Paolo Mar 18 '11 at 19:48
  • I'm assuming you're just showing code from the core as this was added way back in 1.2. – ScottE Mar 18 '11 at 19:56
  • [of](http://api.jquery.com/jQuery.inArray) [course](http://api.jquery.com/category/version/1.2/)! ;) – Paolo Mar 18 '11 at 20:00
  • Does this answer your question? [Check if an array contains any element of another array in JavaScript](https://stackoverflow.com/questions/16312528/check-if-an-array-contains-any-element-of-another-array-in-javascript) – Nick is tired Dec 14 '21 at 16:46

2 Answers2

1
arr1.some(function(el) {
    return arr2.indexOf(el) > -1
});

The MDC provides the following code for browsers that don't implement some:

if (!Array.prototype.some)
{
  Array.prototype.some = function(fun /*, thisp */)
  {
    "use strict";

    if (this === void 0 || this === null)
      throw new TypeError();

    var t = Object(this);
    var len = t.length >>> 0;
    if (typeof fun !== "function")
      throw new TypeError();

    var thisp = arguments[1];
    for (var i = 0; i < len; i++)
    {
      if (i in t && fun.call(thisp, t[i], i, t))
        return true;
    }

    return false;
  };
}
Wayne
  • 59,728
  • 15
  • 131
  • 126
  • This needs to be prototyped though, no? – ScottE Mar 18 '11 at 19:18
  • @ScottE - It's available in Firefox. It's probably not in other browsers, but you can see an implementation here: https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/some – Wayne Mar 18 '11 at 19:19
  • @Scott `some` is a new Array method added in EcmaScript 5. It is implemented in IE9, but not earlier versions of IE. – Šime Vidas Mar 18 '11 at 19:19
  • @Sime Vidas - yep, same for indexOf. – ScottE Mar 18 '11 at 19:21
  • I'm looking at about 4ms with my solution for 170 map markers and similar with your solution, so looks like this is likely a premature optimization... but perhaps I'll test with a bunch more to see. – ScottE Mar 18 '11 at 19:33
  • @lwburk - this was actually a bit faster with about 30 filters and 170 markers. We're still talking only a few milliseconds. – ScottE Mar 18 '11 at 19:56
0

Sort both arrays, and after that use this pseudocode:

i=0;
j=0;
while(i<array1length && j<array2length) {
    if (array1[i]<array2[j])
        i++;
    else if (array1[i]>array2[j])
        j++;
    else
        //match!!
}

I think, there is no faster way to do this.

Eventually, if you are matching strings, you may hashcode them first, and match only the hashcodes.

Artur Iwan
  • 1,151
  • 2
  • 10
  • 17