0

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");
4
  • 1
    There's probably at least 3 ways to do that, not counting libs like JQuery Commented Mar 23, 2013 at 0:57
  • any directions please? Commented Mar 23, 2013 at 0:58
  • Why didn't SO put the info that there where more answers added? Commented Mar 23, 2013 at 1:05
  • 2
    Just a hint about style, ["Saab", "Volvo", "BMW"] is funtionally equivalent. Using new Array() is ugly. Commented Mar 23, 2013 at 1:07

7 Answers 7

2

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

1 Comment

See the bottom of the link
2

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

Comments

1

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

Why not just return true? Why bother going through all elements when all you care about is if one works?
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
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 :)
0

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.

Comments

0

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

1 Comment

If your browser doesn't support it you should just include the code from the linked page...
0

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>

Comments

-3

There are several ways you can do this:

indexOf()

var cars_1 = ["Saab","Volvo","BMW"];
var cars_2 = ["Honda","Mazda","BMW", "suzuki"];

cars_1.indexOf("BMW") != -1
// true

1 Comment

well, let me explain my self better, i need to know if any, now matter which, but any value is exist

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.