I want to check condition inside array with single object.
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,
};
i want to compare the obj within the array and if its match one, i want to get the name. for ex, obj is equal to marry. i want to print mary. This is what i have tried.
let result = arr.filter((item) => item.description === obj )
console.log(result.name);
You could build a filter with the entries of the object and iterate all entries and check the values. Then map the names.
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 } }], object = { height: 5.8, weight: 65 }, filters = Object.entries(object), result = array.filter(({ description }) => filters.every(([key, value]) => description[key] === value)).map(({ name }) => name); console.log(result);
You have to compare the desired keys against each other, rather than comparing the objects, as object equality is based upon their memory addresses being the same.
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 }; console.log( arr.filter(item => item.description.height === obj.height && item.description.weight === obj.weight) );
you can narrow down the the property that you want to filter on like this
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)
One approach would be using Object.keys
.
var 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, }}]; var obj = { height: 5.8, weight: 65 }; var result = arr.filter(({description})=>Object.keys(description).every(k=>description[k]==obj[k])).map(({name})=>name); console.log(result);
The root cause for the failure of filter filter condition is due to the comparison between objects being attempted.
Comparison of objects can be done by using a helper function.
Here is the working 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)
Output:
$ node array-filter-demo.js
[ { name: 'mary', description: { height: 5.8, weight: 65 } } ]
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.