比较2个数组,如果匹配则返回1

时间:2015-02-14 19:52:45

标签: javascript jquery arrays regex

当我控制下面的两个数组是它的样子

aa=  [0: "349",1: "810",2: "863",3: "657",4: "602",5: "317",6: "665",7: "865",8: "624",9: "805",10: "887",11: "659"];

bb=[0: "349", 1: ""];

cc=find_flight(aa,bb);
两个阵列中的

是匹配的,一旦匹配就应该返回1.以下是我试过的方法

function find_flight(aa,bb)
    {
        if(a2.toString() == a1.toString())
        {
                return true;
        }
    }


console.log(aa+'-'+bb);

Output: 349,810,863,657,602,317,665,865,624,805,887,659-349,

它不匹配可以任何人给出解决方案!!!!

2 个答案:

答案 0 :(得分:1)

function find_flight(a,b)
{
var check  = false;
a.forEach(function(x) { 
   b.forEach(function(y) { 
       //console.log(x+"  "+y +"   "+(x==y?1:0));
       if (x==y) { check=true; } 
   });
});
return check;
}

var aa= [1,2,3,4,5,6];
var bb= [0,9,8,7, 4];

一样使用它
console.log(find_flight(aa,bb));

答案 1 :(得分:0)

一种方法是:

found=false; 
aa.forEach(function(e) { 
   bb.forEach(function(e2) { 
     if (e === e2) { found=true; } 
   });
});

我用它来初始化数组,因为我的控制台不接受你的语法。

aa = ["349","810","863","657","602","317","665","865","624","805","887","659"];
bb = ["349",""];