0

假设,我们有两个数组,

 array1 = [1,2,3,6,3,5,2,5,2,4,3]
 array2 = [3,4,5]

如何找到位于 array2 内的值“3”,然后从 array1 进行比较任何帮助表示赞赏。

谢谢。

4

4 回答 4

1

尝试

var arrayA = [1,2,3,6,3,5,2,5,2,4,3];
var arrayB = [3,4,5];
var arrayC = []; 

$('.arrayA').text('ArrayA: ' + arrayA);
$('.arrayB').text('ArrayB: ' + arrayB);

$.each(arrayA, function(indexA,valueA) {
    $.each(arrayB, function(indexB, valueB){
        if(valueA == valueB)
        {
            alert(valueA);
            alert(valueB);            
            return false;             
        }
    });
});
于 2013-07-31T13:01:37.590 回答
1

类似的问题:How to remove specific value from array using jQuery

从链接页面(用您的值替换它们的值):

var array1 =[1,2,3,6,3,5,2,5,2,4,3];
var array2 =[1,2,3] 
var removeItem = array2[jQuery.inArray(3,array2)]; 

//jQuery.inArray returns the index where the value (3) first appears in the array (array2)

array1 = jQuery.grep(array1, function(value) {
    return value != removeItem;
});
console.log(array1); //[1,2,6,5,2,5,2,4]
于 2013-07-31T12:30:52.247 回答
1

尝试这个:

function checkValueInArray(x){
    if(array1.indexOf(x) != -1 && array2.indexOf(x) != -1){
        //both arrays have this value and there indexes are
        //array1.indexOf(x);
        //array2.indexOf(x);
    }
}

checkValueInArray("3");
于 2013-07-31T12:32:30.053 回答
0

尝试使用索引

if(array1.indexOf("3") != -1){
   //exists
}
于 2013-07-31T12:20:49.667 回答