Hash的迭代:JekyII

时间:2017-03-31 11:02:35

标签: for-loop iteration jekyll shopify liquid

您好我试图提取这个(" Polk County"," short_name" =>" Polk County" ) 在这个哈希的第3排,但我似乎只能得到#34;波尔克县" 这是我目前的代码:

{% 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"]
}

}

2 个答案:

答案 0 :(得分:0)

虽然我不清楚你在这里要问的是什么,但是下面的代码

{% for county_hash in location.address_components %}
  {% for county in county_hash %}
    {{ county.long_name }}
  {% endfor %}
{% endfor %}

returnlong_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 %} 过滤器:

{{1}}

答案 1 :(得分:0)

感谢您的帮助, 这种方法对我有用。

{% for county_hash in location.address_components %}
  {% for county in county_hash %}
    {% if county.long_name contains 'County' %}
     {{ county.long_name }}
    {% endif %}
  {% endfor %}
{% endfor %}

breadcrumbs