假设我们有两个数组
array1 = [1,2,3,6,3,5,2,5,2,4,3]
array2 = [3,4,5]
如何找到位于array2内的值“3”,然后比较array1中的相同内容。感谢任何帮助。
感谢。
答案 0 :(得分:1)
类似的问题:How to remove specifc 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]
答案 1 :(得分: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");
答案 2 :(得分: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;
}
});
});
答案 3 :(得分:0)
尝试索引
if(array1.indexOf("3") != -1){
//exists
}