我有一个值对象Destructure
,但是在这样做之前,我想先检查一下该对象是否可用,
const { contactno, contactemail } = this.props.app.user;
在这种情况下,该对象user
并非始终可用,因此,我收到以下错误消息,
TypeError: Cannot read property 'contactno' of undefined.
因此,有没有办法检查对象之前是否可用Destructure
?
使用AND和OR运算符,您可以像这样安全地分解对象。
const { contactno, contactemail } = (this.props.app && this.props.app.user) || {};
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句