简体   繁体   English

仅返回值 if.includes() 使用.find() 找到一些东西

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM