SO-33461584
by David Thomas
JavaScript
// 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