目前我正在进行 JavaScript 练习。
练习的目的是比较不同对象形成的数组。该函数有 2 个参数:集合是主数组,源是比较标准。仅返回主数组中包含键和与源数组相同属性的对象。
结果应如下:
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" });
但是,当我在控制台中测试条件时,它返回以下内容:
[{…}, {…}, {…}]
同时,我看到有一个类似的答案。但不同之处在于,它使用否定选择,当存在不匹配的情况时返回 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" });
这次成功了。
所以我想知道的是,第一个脚本的错误是什么? 如果我仍然想使用肯定标准,即只返回符合这两个标准的案例,我该怎么做? 非常感谢
最佳答案
在不起作用的原始函数中,您始终从 filter
回调返回 true
,因此不会过滤掉任何内容。
但是,您还应该能够通过搜索对象的 Object.keys
过滤和使用 every
:
function whatIsInAName(arr, search_obj) {
return arr.filter(obj => Object.keys(search_obj)
.every(key => obj[key] === search_obj[key]))
}
console.log(whatIsInAName([{ first: "Romeo", last: "Montague" }, { first: "Mercutio", last: null }, { first: "Tybalt", last: "Capulet" }], { last: "Capulet" }))
// [{ first: "Tybalt", last: "Capulet" }].
console.log(whatIsInAName([{ "apple": 1 }, { "apple": 1 }, { "apple": 1, "bat": 2 }], { "apple": 1 }))
// [{ "apple": 1 }, { "apple": 1 }, { "apple": 1, "bat": 2 }]
console.log(whatIsInAName([{ "apple": 1, "bat": 2 }, { "bat": 2 }, { "apple": 1, "bat": 2, "cookie": 2 }], { "apple": 1, "bat": 2 }))
// [{ "apple": 1, "bat": 2 }, { "apple": 1, "bat": 2, "cookie": 2 }]
console.log(whatIsInAName([{ "apple": 1, "bat": 2 }, { "apple": 1 }, { "apple": 1, "bat": 2, "cookie": 2 }], { "apple": 1, "cookie": 2 }))
// [{ "apple": 1, "bat": 2, "cookie": 2 }]
console.log(whatIsInAName([{ "apple": 1, "bat": 2 }, { "apple": 1 }, { "apple": 1, "bat": 2, "cookie": 2 }, { "bat":2 }], { "apple": 1, "bat": 2 }))
// [{ "apple": 1, "bat": 2 }, { "apple": 1, "bat": 2, "cookie":2 }]
console.log(whatIsInAName([{"a": 1, "b": 2, "c": 3}], {"a": 1, "b": 9999, "c": 3}))
// []
关于javascript - 在具有多个对象的数组中应用数组过滤器时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52021315/