Javascript 使用另一个数组检查数组中的多个值
我有一个从JSON数据获得的对象数组Javascript 使用另一个数组检查数组中的多个值,javascript,Javascript,我有一个从JSON数据获得的对象数组A,如下所示: [Object { field1="2381", field2="1233", field3="46.44852", more...}, Object { field1="2381", field2="1774", field3="45.70752833333334", more...}] 我还有另一个数组B类似 ["2381", "1187"] 是否有方法检查此数组B的值是否存在于数组a中 我试过用类似的东西 A.map((B[0], B
A
,如下所示:
[Object { field1="2381", field2="1233", field3="46.44852", more...},
Object { field1="2381", field2="1774", field3="45.70752833333334", more...}]
我还有另一个数组B
类似
["2381", "1187"]
是否有方法检查此数组B
的值是否存在于数组a
中
我试过用类似的东西
A.map((B[0], B[1]), function(element) {
if (B[0] == element.field1 && B[1] == element.field2)
return true;
else
return false;
});
但效果不太好
有什么窍门吗?您可以使用如下函数:
function check(arr, fn) {
var i = 0, l = arr.length;
for (; i < l; i++) {
if (fn(arr[i])) return true;
}
return false;
}
查看A中是否至少有一项与B匹配
A.some(function(value) {
return value.field1 == B[0] && value.field2 == B[1];
});
可以使用嵌套的for循环。大致如下:
var checker; //number to hold the current value
for(x=0;x<arrayA.length; x++) //loop through the first array
{
checker = arrayA[x]; store the number in each element in the variable
for(y=array.Indexof(arrayA, checker);y<arrayB.length; y++) /*don't start from the very first index of the array but rather from the last place where arrayA was*/
{
if (arrayB[y] == checker)
{
Alert(checker + " is the same");
}//closes if
}//closes for
}//closes outer for loop
var校验器//用于保存当前值的数字
对于(x=0;xSee)这是否有帮助,您的意思是arr是我的数组A,fn是数组B?
var checker; //number to hold the current value
for(x=0;x<arrayA.length; x++) //loop through the first array
{
checker = arrayA[x]; store the number in each element in the variable
for(y=array.Indexof(arrayA, checker);y<arrayB.length; y++) /*don't start from the very first index of the array but rather from the last place where arrayA was*/
{
if (arrayB[y] == checker)
{
Alert(checker + " is the same");
}//closes if
}//closes for
}//closes outer for loop