I use this script:
var place = autocomplete.getPlace();
latitude = place.geometry.location.lat();
longitude = place.geometry.location.lng();
street_number =
You shouldn't rely on index of element in place.address_components array. Instead you have to analyze the "types" field to find the corresponding address component for the country.
It can be done with Array.prototype.filter() and Array.prototype.includes() functions.
var filtered_array = place.address_components.filter(function(address_component){
return address_component.types.includes("country");
});
var country = filtered_array.length ? filtered_array[0].long_name: "";