how can i check if at least one value from the first array exists on the second array?, for example, how can i check if there BMW on car_1 array and car_2 array?
var cars_1 = new Array("Saab","Volvo","BMW");
var cars_2 = new Array("Honda","Mazda","BMW", "suzuki");
Kijewski
26.1k14 gold badges108 silver badges149 bronze badges
7 Answers 7
Complete solution using some:
// will return true if at least one element of cars_1 is in cars_2
cars_1.some(function (e) {
return cars_2.indexOf(e) >= 0;
});
answered Mar 23, 2013 at 1:01
beatgammit
20.3k20 gold badges92 silver badges131 bronze badges
Sign up to request clarification or add additional context in comments.
1 Comment
beatgammit
See the bottom of the link
One quick and easy way:
function overlap(arr1,arr2) {
for(var i = 0; i < arr1.length; ++i)
if(arr2.indexOf(arr1[i]) != -1)
return true;
return false;
}
answered Mar 23, 2013 at 1:02
Appleshell
7,4387 gold badges56 silver badges100 bronze badges
Comments
a quick easy answer:
for (var i =0; i<cars_1.length; i++){
for (var j=0; j<cars_2.length; j++){
if(cars_2[j] == cars_1[i]) return true;
}
}
return false;
Edit: ok, more efficient in response to the comment :) Edit2: ok, even more efficient :)
3 Comments
beatgammit
Why not just return true? Why bother going through all elements when all you care about is if one works?
Ryan
lol, i put about 3 seconds into it lol, the point was, this works, I wasn't necessarily trying to win the javascript speed contest... here let me give it a second thought
Ryan
in theory i could make it even faster by wrapping it all into a single loop, saving off the .length vars, and performing as much as possible inside of the parenthesis, but it would be way less readable, and probably more than the OP needs :)
A simple O(m+n) solution:
var cars_1 = ["Saab","Volvo","BMW"];
var cars_2 = ["Honda","Mazda","BMW", "suzuki"];
// build up a hash table of the cars in the 1st sequence:
var set_1 = {};
for (var i = 0; i < cars_1.length; ++i) {
set_1[cars_1[i]] = true;
}
// look if there is a car in sequence 2 that is in set_1:
var has_intersection = false;
for (var i = 0; i < cars_2.length; ++i) {
if (set_1[cars_2[i]] === true) {
has_intersection = true;
break;
}
}
Works in any browser, too.
answered Mar 23, 2013 at 1:04
Kijewski
26.1k14 gold badges108 silver badges149 bronze badges
Comments
If your browser supports it, you can use the .some method.
var cars1 = [...],
cars2 = [...];
var res = cars1.some(function(a) {
return cars2.indexOf(a) > -1;
});
If not then you can create your own shim:
function some(list, callback) {
var len = list.length;
for (var i = len; i--;) {
if (callback(list[i], i)) {
return true;
}
}
return false;
}
var res = some(cars1, function(a) {
return cars2.indexOf(a) > -1;
});
answered Mar 23, 2013 at 1:04
David G
97.6k41 gold badges173 silver badges258 bronze badges
1 Comment
Esailija
If your browser doesn't support it you should just include the code from the linked page...
You can also use 'filter' method:
var cars_1 = new Array("Saab","Volvo","BMW");
var cars_2 = new Array("Honda","Mazda","BMW", "suzuki");
var cars_3 = new Array("Audi");
/* with underscore.js */
var _hasIntersection = function (arr1, arr2) {
var intArr = _.filter(arr1, function (elem) { return arr2.indexOf(elem) > -1 });
return intArr.length;
}
/* or using Array.prototype.filter */
var hasIntersection = function (arr1, arr2) {
var intArr = arr1.filter(function (elem) { return arr2.indexOf(elem) > -1 });
return intArr.length;
}
console.log(_hasIntersection(cars_1, cars_2)); // 1
console.log(hasIntersection(cars_1, cars_2)); // 1
console.log(hasIntersection(cars_1, cars_3)); // 0
<script src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.7.0/underscore-min.js"></script>
answered Sep 25, 2014 at 9:14
Nico Napoli
1,8872 gold badges13 silver badges12 bronze badges
Comments
There are several ways you can do this:
var cars_1 = ["Saab","Volvo","BMW"];
var cars_2 = ["Honda","Mazda","BMW", "suzuki"];
cars_1.indexOf("BMW") != -1
// true
Kijewski
26.1k14 gold badges108 silver badges149 bronze badges
answered Mar 23, 2013 at 0:58
Hunter McMillen
61.8k25 gold badges124 silver badges176 bronze badges
1 Comment
Avi Nahum
well, let me explain my self better, i need to know if any, now matter which, but any value is exist
lang-js
["Saab", "Volvo", "BMW"]is funtionally equivalent. Usingnew Array()is ugly.