JavaScript——在对象中搜索数组值
我有一个数组和一个对象数组。我想在对象数组中搜索数组中的每个值,如果它不存在,我想从数组中删除该值JavaScript——在对象中搜索数组值,javascript,Javascript,我有一个数组和一个对象数组。我想在对象数组中搜索数组中的每个值,如果它不存在,我想从数组中删除该值 var arr= [1,2,3,4]; var recs = [{id:1},{id:2},{id:3}]; //4 doesnt exist in recs, remove from arr //arr = [1,2,3]; 这是我的尝试。显然不起作用。在继续下一个arr索引之前,我不确定如何将每个arr索引与REC中的所有值进行比较: var arr= [1, 2, 3, 4], ind
var arr= [1,2,3,4];
var recs = [{id:1},{id:2},{id:3}]; //4 doesnt exist in recs, remove from arr
//arr = [1,2,3];
这是我的尝试。显然不起作用。在继续下一个arr索引之前,我不确定如何将每个arr索引与REC中的所有值进行比较:
var arr= [1, 2, 3, 4], index;
var recs = [{a:1},{a:2},{a:3}];
for(var i = 0; i<arr.length; i++){
for(var val in recs[i]){
if(arr[i] != recs[i][val]){
index = arr.indexOf(arr[i]);
arr.splice(index, 1);
}
}
}
var-arr=[1,2,3,4],索引;
var recs=[{a:1},{a:2},{a:3}];
对于(var i=0;i,如果您不介意单独保留原始数组实例并创建一个新实例(基本上将其视为不可变的)
所用方法的详细信息:Array.filter
传递一个函数,对其中的每个元素运行该函数,然后返回一个新数组,其中只包含函数中返回true的元素
var arr= [1, 2, 3, 4];
var recs = [{a:1},{a:2},{a:3}];
// don't use var in for loops.
// Variables declared with var have function scope, so declare them at the top of your function
var i;
var j;
var value;
var found;
// iterate over the array
for (i = 0; i < arr.length; i++)
{
value = arr[i];
found = false;
// iterate over the other array
for (j = 0 ; j < recs.length ; j++)
{
// if we found what we were looking for, make a note and exit loop
if (recs[j].a == value)
{
found = true;
break;
}
}
if (!found)
{
arr.splice(i, 1);
// To compensate the loop's i++, to avoid skipping the next entry
i--;
}
}
alert(arr.join(','));
数组。每个
的工作原理有点类似,但返回布尔值,true或false。如果数组中的所有元素在函数内部都返回了true
,则返回true。如果您愿意单独保留原始数组实例并创建一个新实例(基本上将其视为不可变的)
var arr= [1, 2, 3, 4];
var recs = [{a:1},{a:2},{a:3}];
// don't use var in for loops.
// Variables declared with var have function scope, so declare them at the top of your function
var i;
var j;
var value;
var found;
// iterate over the array
for (i = 0; i < arr.length; i++)
{
value = arr[i];
found = false;
// iterate over the other array
for (j = 0 ; j < recs.length ; j++)
{
// if we found what we were looking for, make a note and exit loop
if (recs[j].a == value)
{
found = true;
break;
}
}
if (!found)
{
arr.splice(i, 1);
// To compensate the loop's i++, to avoid skipping the next entry
i--;
}
}
alert(arr.join(','));
所用方法的详细信息:Array.filter
传递一个函数,对其中的每个元素运行该函数,然后返回一个新数组,其中只包含函数中返回true的元素
var arr= [1, 2, 3, 4];
var recs = [{a:1},{a:2},{a:3}];
// don't use var in for loops.
// Variables declared with var have function scope, so declare them at the top of your function
var i;
var j;
var value;
var found;
// iterate over the array
for (i = 0; i < arr.length; i++)
{
value = arr[i];
found = false;
// iterate over the other array
for (j = 0 ; j < recs.length ; j++)
{
// if we found what we were looking for, make a note and exit loop
if (recs[j].a == value)
{
found = true;
break;
}
}
if (!found)
{
arr.splice(i, 1);
// To compensate the loop's i++, to avoid skipping the next entry
i--;
}
}
alert(arr.join(','));
数组。每个
的工作原理有点类似,但返回一个布尔值,true或false。如果数组中的所有元素在函数内部都返回了true
。var arr=[1,2,3,4];
var arr= [1, 2, 3, 4];
var recs = [{a:1},{a:2},{a:3}];
// don't use var in for loops.
// Variables declared with var have function scope, so declare them at the top of your function
var i;
var j;
var value;
var found;
// iterate over the array
for (i = 0; i < arr.length; i++)
{
value = arr[i];
found = false;
// iterate over the other array
for (j = 0 ; j < recs.length ; j++)
{
// if we found what we were looking for, make a note and exit loop
if (recs[j].a == value)
{
found = true;
break;
}
}
if (!found)
{
arr.splice(i, 1);
// To compensate the loop's i++, to avoid skipping the next entry
i--;
}
}
alert(arr.join(','));
var recs=[{a:1},{a:2},{a:3}];
//不要在for循环中使用var。
//使用var声明的变量具有函数作用域,因此在函数顶部声明它们
var i;
var j;
var值;
var发现;
//迭代数组
对于(i=0;ivar-arr=[1,2,3,4];
var recs=[{a:1},{a:2},{a:3}];
//不要在for循环中使用var。
//使用var声明的变量具有函数作用域,因此在函数顶部声明它们
var i;
var j;
var值;
var发现;
//迭代数组
对于(i=0;i
abc123的可能重复我认为问题更多的是通过数组循环和删除元素(困难部分),而不是查找对象。@Katana314问题中的代码有很多问题,很难说主要问题是什么:-)@Katana314上面的答案2从字面上显示了如何通过一个循环创建他想要的数组…删除将是额外的一步。@abc123的可能重复我认为问题更多的是通过数组循环和删除元素(困难部分)与查找对象相比,@Katana314问题中的代码有很多问题,很难说主要问题是什么:-)@Katana314上面的答案2从字面上说明了如何创建他想要的数组…删除将是额外的一步。