I am using React framework.
Here is what I have:
const myObject = this.props.objects.find(({id}) => ({id} === this.state.selectedObject.id));
const name = !isEmpty(myObject) ? myObject.name : '';
I avoid doing the following:
const name = !isEmpty(this.props.objects.find(({id}) => ({id} === this.state.selectedObject.id))) ? this.props.objects.find(({id}) => ({id} === this.state.selectedObject.id)).name : '';
because it doesn't make sense to execute find() twice. But I'm just wondering if there's some nice syntax that can make this a one-liner that I'm missing.
{id} === this.state.selectedObject.id? its never true.findresult as an argument to an IIFE, but that's arguably even less readable that your second formulation:const name = (o=>!isEmpty(o)?o.name:'')(this.props.objects.find(...))