[英]Only returning value if .includes() finds something using .find()
I have this function set up, consuming a Google Maps API response:我有这个 function 设置,使用谷歌地图 API 响应:
zipCode: addressObject.address_components.find((component) =>
component.types.includes("postal_code")
).long_name,
However, the postal_code
is not in the types
, so how do I only retrieve the long_name
when the types
array includes postal_code
?但是,
postal_code
不在types
中,那么当types
数组包含postal_code
时,如何仅检索long_name
?
Use the optional chaining ( ?.
) operator to get undefined
when no item is found:未找到任何项目时,使用可选的链接 (
?.
)运算符获取undefined
:
zipCode: addressObject.address_components.find((component) =>
component.types.includes("postal_code")
)?.long_name,
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.