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.
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.
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
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));
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));
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;
}
and now we can check
if(hasCommonElement(arr1,arr2)){
return true;
}
Hopefully there would be a better answer...
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