我正在编写一个离子应用程序 - 所以这是角度1.x
我有两个数字数组
var arr1 = [1,32,423,43,23,64,232,5,67,54];
var arr2 = [11,32,1423,143,123,64,2232,35,467,594];
数组32中有两个常用数字,64位。
我只是想要一些javascript来有效地返回true,如果有的话 2个数组中至少有1个常用数字。
我有以下代码
angular.forEach(arr1 , function (arr1 , count) {
if ( inArray(arr1 , arr2) )
{
return true;
}
});
答案 0 :(得分:2)
var arr1 = [1, 32, 423, 43, 23, 64, 232, 5, 67, 54];
var arr2 = [11, 32, 1423, 143, 123, 64, 2232, 35, 467, 594];
function hasCommonNumbers(arr1, arr2) {
let found = false;
for (let i = 0; i < arr1.length; i++) {
if (arr2.indexOf(arr1[i]) !== -1) {
found = true;
break;
}
}
return found;
}
console.log(hasCommonNumbers(arr1,arr2));
答案 1 :(得分:2)
与ES6相比,这是微不足道的:
var arr1 = [1, 32, 423, 43, 23, 64, 232, 5, 67, 54];
var arr2 = [11, 32, 1423, 143, 123, 64, 2232, 35, 467, 594];
console.log(arr1.some(i => arr2.includes(i)));
&#13;
答案 2 :(得分:0)
var arr1 = [1,32,423,43,23,64,232,5,67,54];
var arr2 = [11,32,1423,143,123,64,2232,35,467,594];
var hasDupe = arr1.some(function(val){
return arr2.includes(val);
});
console.log(hasDupe);