Javascript 在具有多个对象的数组中应用数组筛选器时出错
目前我正在做一个JavaScript练习 练习的目的是比较由不同对象形成的阵列。函数有两个参数:集合是主数组,源是比较条件。只返回主数组中包含键和与源数组相同属性的对象 结果应如下:Javascript 在具有多个对象的数组中应用数组筛选器时出错,javascript,arrays,array-filter,Javascript,Arrays,Array Filter,目前我正在做一个JavaScript练习 练习的目的是比较由不同对象形成的阵列。函数有两个参数:集合是主数组,源是比较条件。只返回主数组中包含键和与源数组相同属性的对象 结果应如下: whatIsInAName( [{ first: "Romeo", last: "Montague" }, { first: "Mercutio", last: null }, { first: "Tybalt", last: "Capulet" }], { last: "Capulet" } ) // s
whatIsInAName(
[{ first: "Romeo", last: "Montague" }, { first: "Mercutio", last: null }, { first: "Tybalt", last: "Capulet" }],
{ last: "Capulet" }
) // should return [{ first: "Tybalt", last: "Capulet" }].
whatIsInAName(
[{ "apple": 1 }, { "apple": 1 }, { "apple": 1, "bat": 2 }],
{ "apple": 1 }
) // should return [{ "apple": 1 }, { "apple": 1 }, { "apple": 1, "bat": 2 }]
whatIsInAName(
[{ "apple": 1, "bat": 2 }, { "bat": 2 }, { "apple": 1, "bat": 2, "cookie": 2 }],
{ "apple": 1, "bat": 2 }
) // should return [{ "apple": 1, "bat": 2 }, { "apple": 1, "bat": 2, "cookie": 2 }]
whatIsInAName(
[{ "apple": 1, "bat": 2 }, { "apple": 1 }, { "apple": 1, "bat": 2, "cookie": 2 }],
{ "apple": 1, "cookie": 2 }
) // should return [{ "apple": 1, "bat": 2, "cookie": 2 }]
whatIsInAName(
[{ "apple": 1, "bat": 2 }, { "apple": 1 }, { "apple": 1, "bat": 2, "cookie": 2 }, { "bat":2 }],
{ "apple": 1, "bat": 2 }
) // should return [{ "apple": 1, "bat": 2 }, { "apple": 1, "bat": 2, "cookie":2 }]
whatIsInAName(
[{"a": 1, "b": 2, "c": 3}],
{"a": 1, "b": 9999, "c": 3}
) // should return []
function whatIsInAName(collection, source) {
var arr = [];
var key = Object.keys(source)
return collection.filter(function(x){
for(var y = 0; y < key.length; y++){
if(x.hasOwnProperty(key[y]) && x[key[y]] == source[key[y]]){
return true;
}
}
return true;
})
}
whatIsInAName([{ first: "Romeo", last: "Montague" }, { first: "Mercutio", last: null }, { first: "Tybalt", last: "Capulet" }], { last: "Capulet" });
为了解决这个问题,我使用数组过滤器函数,目的是选择符合条件的对象,并作为新数组返回。代码如下:
whatIsInAName(
[{ first: "Romeo", last: "Montague" }, { first: "Mercutio", last: null }, { first: "Tybalt", last: "Capulet" }],
{ last: "Capulet" }
) // should return [{ first: "Tybalt", last: "Capulet" }].
whatIsInAName(
[{ "apple": 1 }, { "apple": 1 }, { "apple": 1, "bat": 2 }],
{ "apple": 1 }
) // should return [{ "apple": 1 }, { "apple": 1 }, { "apple": 1, "bat": 2 }]
whatIsInAName(
[{ "apple": 1, "bat": 2 }, { "bat": 2 }, { "apple": 1, "bat": 2, "cookie": 2 }],
{ "apple": 1, "bat": 2 }
) // should return [{ "apple": 1, "bat": 2 }, { "apple": 1, "bat": 2, "cookie": 2 }]
whatIsInAName(
[{ "apple": 1, "bat": 2 }, { "apple": 1 }, { "apple": 1, "bat": 2, "cookie": 2 }],
{ "apple": 1, "cookie": 2 }
) // should return [{ "apple": 1, "bat": 2, "cookie": 2 }]
whatIsInAName(
[{ "apple": 1, "bat": 2 }, { "apple": 1 }, { "apple": 1, "bat": 2, "cookie": 2 }, { "bat":2 }],
{ "apple": 1, "bat": 2 }
) // should return [{ "apple": 1, "bat": 2 }, { "apple": 1, "bat": 2, "cookie":2 }]
whatIsInAName(
[{"a": 1, "b": 2, "c": 3}],
{"a": 1, "b": 9999, "c": 3}
) // should return []
function whatIsInAName(collection, source) {
var arr = [];
var key = Object.keys(source)
return collection.filter(function(x){
for(var y = 0; y < key.length; y++){
if(x.hasOwnProperty(key[y]) && x[key[y]] == source[key[y]]){
return true;
}
}
return true;
})
}
whatIsInAName([{ first: "Romeo", last: "Montague" }, { first: "Mercutio", last: null }, { first: "Tybalt", last: "Capulet" }], { last: "Capulet" });
函数whatisiname(集合,源){
var-arr=[];
var key=Object.keys(源)
返回集合.过滤器(函数(x){
对于(变量y=0;y
但是,当我在控制台中测试条件时,它返回以下内容:
[{…},{…},{…}]
同时,我看到了一个类似的答案。但不同的是,它使用的是否定选择,当有这样不匹配的东西时返回false:
function whatIsInAName(collection, source) {
var srcKeys = Object.keys(source);
return collection.filter(function (obj) {
for(var i = 0; i < srcKeys.length; i++) {
if(!obj.hasOwnProperty(srcKeys[i]) || obj[srcKeys[i]] !== source[srcKeys[i]]) {
return false;
}
}
return true;
});
}
// test here
whatIsInAName([{ first: "Romeo", last: "Montague" }, { first: "Mercutio", last: null }, { first: "Tybalt", last: "Capulet" }], { last: "Capulet" });
函数whatisiname(集合,源){
var srcKeys=Object.keys(源);
返回集合过滤器(函数(obj){
对于(var i=0;i
这次成功了
所以我想知道的是,我在第一个脚本上的错误是什么?
如果我仍然想使用积极的标准,即只返回符合这两个标准的案例,我如何才能做到这一点?
非常感谢在不起作用的原始函数中,您总是从
filter
回调返回true
,因此没有过滤掉任何内容
但是,您还应该能够对对象筛选并使用每个。搜索对象的键
:
函数whatisiname(arr,搜索对象){
返回arr.filter(obj=>Object.keys(search\u obj)
.every(key=>obj[key]==search\u obj[key]))
}
log(whatisiname([{first:“Romeo”,last:“Montague”},{first:“Mercutio”,last:null},{first:“Tybalt”,last:“Capulet”}],{last:“Capulet”}))
//[{第一个:“蒂伯特”,最后一个:“卡普莱特”}。
log(whatisiname([{“苹果”:1},{“苹果”:1},{“苹果”:1,“蝙蝠”:2}],{“苹果”:1}))
//[{“苹果”:1},{“苹果”:1},{“苹果”:1,“蝙蝠”:2}]
log(whatisiname([{apple:1,bat:2},{bat:2},{apple:1,bat:2,cookie:2}],{apple:1,bat:2}))
//[{“苹果”:1,“蝙蝠”:2},{“苹果”:1,“蝙蝠”:2,“饼干”:2}]
log(whatisiname([{apple:1,bat:2},{apple:1},{apple:1,bat:2,cookie:2}],{apple:1,cookie:2}))
//[{“苹果”:1,“蝙蝠”:2,“饼干”:2}]
log(whatisiname([{apple:1,bat:2},{apple:1},{apple:1,bat:2,cookie:2},{apple:1,bat:2}]))
//[{“苹果”:1,“蝙蝠”:2},{“苹果”:1,“蝙蝠”:2,“饼干”:2}]
log(whatisiname([{a:1,b:2,c:3}],{a:1,b:9999,c:3}))
//[]
您的原始代码不起作用,因为只要属性符合条件,您就会返回true
。它不会继续检查其余的钥匙
例如,x
是{a:1,b:2}
,源代码是{a:1,b:1}
。原始代码检查属性a
,它匹配,因此true
从filter
返回。它不会继续检查b
是否匹配(因为return
会立即终止函数)
第二个代码示例正好相反,它在发现一个不匹配的属性时停止检查属性(如果一个属性不匹配,那么检查其余属性将是多余的)
原始代码:搜索符合条件的属性。如果找到一个,则返回true
,如果未找到,则返回false
。只有当所有键都不匹配时,此代码才会失败;如果至少有一个键匹配,则此代码会成功
第二个代码:搜索不匹配的属性。如果找到一个,则返回false
,如果未找到,则返回true
。如果至少有一个键不匹配,此代码将失败;如果所有键都匹配,此代码将成功。以秒数击败我,干得好!顺便说一句,obj[key]&&
是完全不必要的,如果属性为0
,false
。。。等等:whatisiname([{a:0},{a:0,b:1}],{a:0})
hi ibrahim,这简直太棒了,真的很有帮助!还有一个问题:据我所知,过滤器将扫描数组中的所有元素。在我理解之前,在这种情况下,它只会扫描数组中的所有对象,但过滤器会扫描每个对象中的键和属性,确实是吗?@PakHangLeungfilter
会扫描整个数组,但应该扫描对象的是您的函数filter
在数组上循环,并在数组中的每个对象上调用您为其指定的函数,然后查看该函数调用是否生成true
或false
,以决定是否应包含该对象filter
不知道函数做什么,也不关心它,它只关心它的返回