2013-03-23 104 views
0

如何檢查第一個數組中是否存在至少一個數值? 例如,如何檢查car_1數組和car_2數組上是否有寶馬?檢查數組值是否存在於其他陣列

var cars_1 = new Array("Saab","Volvo","BMW"); 
var cars_2 = new Array("Honda","Mazda","BMW", "suzuki"); 
+1

很可能有至少三種方法來做到這一點,不包括像JQuery – Ryan 2013-03-23 00:57:41

+0

任何方向的庫? – 2013-03-23 00:58:17

+0

爲什麼沒有把信息放在那裏有更多答案? – kay 2013-03-23 01:05:07

回答

-2

有幾種方法可以做到這一點:使用some

indexOf()

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

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

好吧,讓我更好地解釋我的自我,我需要知道是否有任何現在的問題,但任何價值都存在 – 2013-03-23 00:59:52

2

完整的解決方案:

// 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; 
}); 
+0

它適用於所有主流瀏覽器嗎? – 2013-03-23 01:02:40

+2

查看[鏈接]的底部(https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Array/some#Browser_compatibility) – tjameson 2013-03-23 01:03:02

0

一個快速簡便的方法:

function overlap(arr1,arr2) { 
    for(var i = 0; i < arr1.length; ++i) 
    if(arr2.indexOf(arr1[i]) != -1) 
     return true; 
    return false; 
} 
1

一個快速簡單的答案:

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; 

編輯: OK,響應評論:) EDIT2更高效: OK,甚至更有效的:)

+0

爲什麼不只是返回true?當你所關心的是如果一個人工作時,爲什麼要仔細檢查所有元素? – tjameson 2013-03-23 01:04:25

+0

大聲笑,我把它放大約3秒鐘大聲笑,重點是,這個作品,我不一定試圖贏得JavaScript速度競賽...在這裏讓我再次想到 – Ryan 2013-03-23 01:05:14

+0

在理論上我可以使它甚至通過將它們全部封裝到一個單一的循環中更快,節省了.length變量,並儘可能地在括號內執行,但它的可讀性會降低,並且可能超過OP的需求:) – Ryan 2013-03-23 01:09:00

0

一個簡單的O( m + n)解決方案:

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

也適用於任何瀏覽器。

0

如果您的瀏覽器支持它,您可以使用.some方法。

var cars1 = [...], 
    cars2 = [...]; 

var res = cars1.some(function(a) { 
    return cars2.indexOf(a) > -1; 
}); 

如果沒有,那麼你可以創建自己的墊片:

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

如果您的瀏覽器不支持不支持它,你應該只包括鏈接頁面的代碼... – Esailija 2013-03-23 01:07:09

0

您還可以使用「過濾器」的方法:

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>