Is there a more efficient way to check for an object in an array based on a property, without having to go through multiple checks and avoiding potential errors with the ?
operator?
/**
* An API returns a job object like:
* { id: 123, name: 'The Job', details: [ { detail_name: "Foo", some: "thing" }, { detail_name: "Bar", some: "thing else" } ] }
*/
const fooDetail = job.details.find(attr => {
return attr.detail_name === 'Foo' });
if (fooDetail && fooDetail.detail_name === "Foo") {
// todo process `some: "thing"`
}
It can be cumbersome to perform a find
operation followed by additional checks to avoid errors. Any suggestions for a better or shorter approach?