I have this code
const arr = [{
id: 1,
name: 'anything'
}, {
id: 2,
name: 'something'
}]
const target = [1]
I want to find whether target has 1 and I want the name property value
I'll do
arr.find(o => target.includes(o.id)).name
but this will break if
arr.find(o => target.includes(o.id))
doesn't have the name
property, it break the entire app and throw error.
So how to ensure this from being crashed? I can do this but it's ugly
const somethingValue = arr.find(o => target.includes(o.id)) && arr.find(o => target.includes(o.id)).name
or I have to map to ensure the arr
always have name property, I have to do this if the arr
is dynamic and the south comes from external party.
Any other way to solve this?