Javascript 如何过滤数组中的对象?
我想用单个对象检查数组内部的条件Javascript 如何过滤数组中的对象?,javascript,Javascript,我想用单个对象检查数组内部的条件 let arr = [ { name: "john", description: { height: 5.5, weight: 54, }, }, { name: "mary", description: { height: 5.8, weight: 65, }, }, { name: "smith", description: {
let arr = [
{
name: "john",
description: {
height: 5.5,
weight: 54,
},
},
{
name: "mary",
description: {
height: 5.8,
weight: 65,
},
},
{
name: "smith",
description: {
height: 6.1,
weight: 85,
},
},
];
let obj = {
height: 5.8,
weight: 65,
};
我想比较数组中的obj,如果它匹配,我想得到名称。
对于前男友来说,obj等于结婚。我想印玛丽。
这就是我尝试过的
let result = arr.filter((item) => item.description === obj )
console.log(result.name);
您可以使用对象的条目构建一个过滤器,迭代所有条目并检查值。然后映射名称
let array=[{name:“john”,description:{height:5.5,weight:54}},{name:“mary”,description:{height:5.8,weight:65},{name:“smith”,description:{height:6.1,weight:85}],
对象={身高:5.8,体重:65},
过滤器=对象。条目(对象),
结果=数组
.filter(({description})=>filters.every([key,value])=>description[key]==value))
.map(({name})=>name);
控制台日志(结果)代码>您必须相互比较所需的键,而不是比较对象,因为对象相等是基于它们的内存地址相同
让arr=[
{
姓名:“约翰”,
说明:{
身高:5.5,
体重:54
}
},
{
姓名:“玛丽”,
说明:{
身高:5.8,
体重:65
}
},
{
姓名:“史密斯”,
说明:{
身高:6.1,
体重:85
}
}
];
设obj={
身高:5.8,
体重:65
};
console.log(
arr.filter(item=>item.description.height===obj.height&&item.description.weight===obj.weight)
);代码>您可以像这样缩小要筛选的属性的范围
let arr = [{
name: "john",
description: {
height: 5.5,
weight: 54
}
},
{
name: "mary",
description: {
height: 5.8,
weight: 65
}
},
{
name: "smith",
description: {
height: 6.1,
weight: 85
}
}
];
let obj = {
height: 5.8,
weight: 65
};
const filtered = arr.filter(({ description }) => description.height === obj.height && description.weight === obj.weight);
console.log(filtered)
筛选器条件失败的根本原因是由于正在尝试的对象之间的比较
对象的比较可以通过使用辅助函数来完成
这是正在工作的exmaple:
// array-filter-demo.js
let arr = [
{ name: "john", description: { height: 5.5, weight: 54 } },
{ name: "mary", description: { height: 5.8, weight: 65 } },
{ name: "smith",description: { height: 6.1, weight: 85 } }
]
let obj = { height: 5.8, weight: 65 }
// Helper function to compare objects
function matching(a, b) {
return ( a.height === b.height && a.weight === b.weight)
}
// Call the helper function inside filter
const result = arr.filter(item => (matching(item.description, obj)))
console.log(result)
$ node array-filter-demo.js
[ { name: 'mary', description: { height: 5.8, weight: 65 } } ]
输出:
// array-filter-demo.js
let arr = [
{ name: "john", description: { height: 5.5, weight: 54 } },
{ name: "mary", description: { height: 5.8, weight: 65 } },
{ name: "smith",description: { height: 6.1, weight: 85 } }
]
let obj = { height: 5.8, weight: 65 }
// Helper function to compare objects
function matching(a, b) {
return ( a.height === b.height && a.weight === b.weight)
}
// Call the helper function inside filter
const result = arr.filter(item => (matching(item.description, obj)))
console.log(result)
$ node array-filter-demo.js
[ { name: 'mary', description: { height: 5.8, weight: 65 } } ]
一种方法是使用Object.keys
var arr=[{姓名:“约翰”,描述:{身高:5.5,体重:54,},},{姓名:“玛丽”,描述:{身高:5.8,体重:65,},},{姓名:“史密斯”,描述:{身高:6.1,体重:85,}];
var obj={身高:5.8,体重:65};
var result=arr.filter(({description})=>Object.keys(description).every(k=>description[k]==obj[k]).map(({name})=>name);
控制台日志(结果)
问题出在哪里?如果您想为mary找到一个,为什么不item.name===“mary”
?否则,只有当对象在内存中是完全相同的对象时,广义对象相等才是真的。如果不匹配,item.description==obj
将为false,即使两个对象的键值对匹配。我只知道此人的身高和体重。根据这些信息,我想查找名称。item.description.height==obj.height&&item.description.weight==obj.weight