Dict (map) key access

I’ve come to realize that I’ve banged my head against the same issue in different ways (and so have others) with hugo and key/value collections. First, some examples. Given a basic dict, we can range over both keys and value as documented:

{{ $foodMap := (dict "a" "apple" "b" "banana" "c" "cantaloupe") }}

{{ range $key, $value := $foodMap }}
    {{ $key }}: {{ $value }}
{{ end }}

outputs:

a: apple
b: banana
c: cantaloupe

If you only use one loop variable, it is set to the value of the map:

{{ range $x := $foodMap }}
  <li>{{ $x }}</li>
{{ end }}

outputs:

apple
banana
cantaloupe

This is a little strange, since it is exactly the opposite of what happens in golang where a single loop variable attaches to the key when ranging over a map.

Next what if we want to use where on a map? According to the docs, the function spec says it works on Collections, but the description does only mention arrays. So lets try. The syntax requires a “key”, so lets modify our data structure so there’s a “key” to use:

{{ $foodMap := (dict "fruit" (dict "a" "apple" "b" "bannan" "c" "cantaloupe") "vegetable" (dict "a" "asparagus" "b" "bokchoy" "c" "carrot")) }}
{{ range $k := where $foodMap "fruit" "ne" "apple" }}
    {{ $k }}
{{ end }}

I’m using "apple" as the match because above a single variable attaches to the value. No output. Tried "a", still no luck. Even tried matching against (dict "a" "apple"). No matter what I’ve tried I’ve never gotten a where clause to work on a dict/map.

It seems valid use case, as I can see lots of times when maps are returned filtering would be useful. In all the cases I’ve run into, it would be the keys I’d want to filter on though.

It still seems like there’s some inconsistencies or at least room for improvement in accessing keys of dicts/maps. Am I the only one that’s butted heads against this? If so I’ll just be quiet now.

Last, I’ll note that I know it’s possible to work around everything above in different ways or with multiple loops, but nesting loops continually is always something I try and avoid when possible. There’s plenty of posts about how to work around these things, I’m seeing if it’s worth discussing if there should be a built in way that is less of a work around.

What is your actual question?