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

}
4
  • Out of completeness: inArray in core.js at 1.5.1. Commented Mar 18, 2011 at 19:48
  • I'm assuming you're just showing code from the core as this was added way back in 1.2. Commented Mar 18, 2011 at 19:56
  • of course! ;) Commented Mar 18, 2011 at 20:00
  • Does this answer your question? Check if an array contains any element of another array in JavaScript Commented Dec 14, 2021 at 16:46

2 Answers 2

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;
  };
}
Sign up to request clarification or add additional context in comments.

6 Comments

This needs to be prototyped though, no?
@ScottE - It's available in Firefox. It's probably not in other browsers, but you can see an implementation here: developer.mozilla.org/en/JavaScript/Reference/Global_Objects/…
@Scott some is a new Array method added in EcmaScript 5. It is implemented in IE9, but not earlier versions of IE.
@Sime Vidas - yep, same for indexOf.
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.
|
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.

Comments

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.