Update Key Values in a Dictionary

Still learning here, but need to update the values in the dictionary items in a slice. Using the code below doesn’t work (unexpected := ). Can someone point me as to the right way to do this?

Thanks

{{ range $idx, $item := $gallery }}

    {{ if gt $idx 0 }}
        {{ .prevTitle := (index $gallery (sub $idx 1)).Title }}
    {{ end }}
    {{ if lt (add $idx 1) (len $gallery) }}
        {{ .nextTitle := (index $gallery (add $idx 1)).Title }}
    {{ end }}


{{ end }}

Thanks

Dicts are immutable, there are two options:

  • Use merge to create a new dict
  • Use newScratch (see docs), update as you please, and then use e.g. $scratch.Values to get the map.
1 Like

Got it :slight_smile:

Thanks

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