2

I want to check with JQuery if two arrays have at least one common element and return true. Following line does not work as I want:

 if(jQuery.inArray(array1,array2) > -1) {return true;}

Could you please help me to find the solution.

EducateYourself
  • 971
  • 2
  • 13
  • 32

4 Answers4

5

A more simpler way, using some method

var needle = ['banana', 'apple', 'berry']; var haystack = ['tree', 'chest', 'apple']; needle.some(n => haystack.some(h=> h===n)); // returns true

gophersumit
  • 114
  • 1
  • 3
4

I'd suggest, in plain JavaScript:

// naming the function, defining the
// arguments to pass:
function commonElements(arr1, arr2) {

    // iterating over arr1, using
    // Array.prototype.some() wherein
    // 'el' is the current element of
    // the array over which we're iterating:
    return arr1.some(function (el) {

        // finding the 'el' value in
        // arr2; and comparing the index
        // if the 'el' value isn't found
        // in arr2 it returns -1:
        return arr2.indexOf(el) > -1;
    });
}

var one = [1, 2, 3, 4, 5],
    two = [5, 6, 7, 8, 9];

console.log(commonElements(one, two)); // true

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

var one = [1, 2, 3, 4, 5],
  two = [5, 6, 7, 8, 9];

console.log(commonElements(one, two));

JS Fiddle.

Alternatively the above could be re-written to extend the prototype of the Array:

// extending the prototype of the Array,
// with a named method/function:
Array.prototype.commonElements = function (arr2) {

    // iterating over the 'this' array:
    return this.some(function (el) {

        // looks for the array-value
        // represented by 'el', and
        // comparing the returned index
        // to see if it's greater than
        // -1 (and so is found in 'this' array):
        return arr2.indexOf(el) > -1;
    });
}

var one = [1, 2, 3, 4, 5],
    two = [5, 6, 7, 8, 9];

console.log(one.commonElements(two)); // true

Array.prototype.commonElements = function(arr2) {
  return this.some(function(el) {
    return arr2.indexOf(el) > -1;
  });
}

var one = [1, 2, 3, 4, 5],
  two = [5, 6, 7, 8, 9];

console.log(one.commonElements(two));

JS Fiddle.

David Thomas
  • 249,100
  • 51
  • 377
  • 410
3

One way to do this is to loop through one array and use $.inArray for the second array..

function hasCommonElement(arr1,arr2)
{
   var bExists = false;
   $.each(arr2, function(index, value){

     if($.inArray(value,arr1)!=-1){
        console.log(value);
        bExists = true;
     }

     if(bExists){
         return false;  //break
     }
   });
   return bExists;
}

jSfiddle

and now we can check

if(hasCommonElement(arr1,arr2)){
    return true;
}

Hopefully there would be a better answer...

Sasidhar Boddeti
  • 1,214
  • 1
  • 11
  • 22
Taleeb
  • 1,888
  • 18
  • 25
2

I really like answer from check if an array contains any elements in another array in Javascript question, especially code:

Array.prototype.indexOfAny = function (array) {
    return this.findIndex(function(v) { return array.indexOf(v) != -1; });
}

Array.prototype.containsAny = function (array) {
    return this.indexOfAny(array) != -1;
}

Then you can just write:

needle = ['banana', 'apple', 'berry'];
haystack = ['tree', 'chest', 'apple'];

haystack.containsAny(needle); // return true
Community
  • 1
  • 1
Grzegorz Gajda
  • 2,424
  • 2
  • 15
  • 23