I have this function set up, consuming a Google Maps API response:
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
?
CodePudding user response:
Use the optional chaining (?.
) operator to get undefined
when no item is found:
zipCode: addressObject.address_components.find((component) =>
component.types.includes("postal_code")
)?.long_name,