Unable to access named properties inside a map using range , index

Hi there. Obviously missing something basic but when I try and access a property inside a data file using the key: name rather than just it’s index (eg.0, 1) I keep getting an error.

<index $value "active">: error calling index: cannot index slice/array with type string

This works:

    {{ range $k, $v := .Site.Data.navigation.global.top }}
  {{ range $key, $value := $v }}
    {{ range (index $value 0) }} 
      {{.}} 
    {{ end }}
  {{ end }}
{{ end }}

This doesn't:

    {{ range $k, $v := .Site.Data.navigation.global.top }}
      {{ range $key, $value := $v }}
        {{ range (index $value "active") }} 
          {{.}} 
        {{ end }}
      {{ end }}
    {{ end }}

Data file:

top:
  - product:
    - active: 
        product
        security
        demos

You are creating array of objects under product by adding -. Remove that for direct nested Map

top:
  - product:
-   - active: 
        product
        security
        demos

Try this:

top:
  - product:
      active: 
        product
        security
        demos

Spot on, thank you.

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.