嗨,我试图提取这个("波尔克县","short_name"=>"波尔克县"(在这个哈希的第 3 行,但我似乎只能得到"波尔克县"这是我当前的代码:
{% for county_hash in location.address_components %}
{% for county in county_hash %}
{{ county.long_name }}
{% endfor %}
{% endfor %}
{
"0" => {
"long_name" => "426", "short_name" => "426", "types" => ["street_number"]
}, "1" => {
"long_name" => "Swanee Drive", "short_name" => "Swanee Dr", "types" => ["route"]
}, "2" => {
"long_name" => "Livingston", "short_name" => "Livingston", "types" => ["locality", "political"]
}, "3" => {
"long_name" => "Polk County", "short_name" => "Polk County", "types" => ["administrative_area_level_2", "political"]
}, "4" => {
"long_name" => "Texas", "short_name" => "TX", "types" => ["administrative_area_level_1", "political"]
}, "5" => {
"long_name" => "United States", "short_name" => "US", "types" => ["country", "political"]
}, "6" => {
"long_name" => "77351", "short_name" => "77351", "types" => ["postal_code"]
}, "7" => {
"long_name" => "8238", "short_name" => "8238", "types" => ["postal_code_suffix"]
}
}
虽然我不清楚你到底想在这里问什么,但以下代码
{% for county_hash in location.address_components %}
{% for county in county_hash %}
{{ county.long_name }}
{% endfor %}
{% endfor %}
将return
一串所有long_names
,即您在评论中提到的426 Swanee Drive Livingston Polk County Texas United States 77351 8238
。
如果你只需要Polk County
,你必须使用where
过滤器:
{% for county_hash in location.address_components %}
{% for county in county_hash %}
{{ county.long_name | where: "shortname", "Polk County" }}
{% endfor %}
{% endfor %}
感谢您的帮助,这种方法对我有用。
{% for county_hash in location.address_components %}
{% for county in county_hash %}
{% if county.long_name contains 'County' %}
{{ county.long_name }}
{% endif %}
{% endfor %}
{% endfor %}
面包屑