Skip to content

Instantly share code, notes, and snippets.

@admicaa
Created August 6, 2020 23:27
Show Gist options
  • Save admicaa/abfbb07507b57336fb491b92b90860c2 to your computer and use it in GitHub Desktop.
Save admicaa/abfbb07507b57336fb491b92b90860c2 to your computer and use it in GitHub Desktop.
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;
}
}
@yanvit503
Copy link

the city atribute in the return was always empty so I changed the city array in the ShouldBeComponent to:

 city: [
    "administrative_area_level_2",
 ]

worked fine, you saved me a lot of time, thx

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment