Created
August 6, 2020 23:27
-
-
Save admicaa/abfbb07507b57336fb491b92b90860c2 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
getAddressObject(address_components) { | |
var ShouldBeComponent = { | |
home: ["street_number"], | |
postal_code: ["postal_code"], | |
street: ["street_address", "route"], | |
region: [ | |
"administrative_area_level_1", | |
"administrative_area_level_2", | |
"administrative_area_level_3", | |
"administrative_area_level_4", | |
"administrative_area_level_5" | |
], | |
city: [ | |
"locality", | |
"sublocality", | |
"sublocality_level_1", | |
"sublocality_level_2", | |
"sublocality_level_3", | |
"sublocality_level_4" | |
], | |
country: ["country"] | |
}; | |
var address = { | |
home: "", | |
postal_code: "", | |
street: "", | |
region: "", | |
city: "", | |
country: "" | |
}; | |
address_components.forEach(component => { | |
for (var shouldBe in ShouldBeComponent) { | |
if (ShouldBeComponent[shouldBe].indexOf(component.types[0]) !== -1) { | |
if (shouldBe === "country") { | |
address[shouldBe] = component.short_name; | |
} else { | |
address[shouldBe] = component.long_name; | |
} | |
} | |
} | |
}); | |
return address; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
the city atribute in the return was always empty so I changed the city array in the ShouldBeComponent to:
worked fine, you saved me a lot of time, thx