skip to Main Content

Hi I was trying to extract this (“Polk County”, “short_name” => “Polk County”)
in the 3rd row of this Hash but I can seem to get just the “Polk County”
this is my current code:

{% 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

Answers


  1. While I’m not clear on what exactly you are trying to ask here, the following code

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

    will return a string of all long_names namely 426 Swanee Drive Livingston Polk County Texas United States 77351 8238 like you mentioned in the comments.


    If you need to get just Polk County, you’ll have to use the where filter:

    {% for county_hash in location.address_components %}
      {% for county in county_hash %}
        {{ county.long_name | where: "shortname", "Polk County" }}
      {% endfor %}
    {% endfor %}
    
    Login or Signup to reply.
  2. Thanks for the help,
    This method works for me.

    {% 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

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search