# Is it possible to change map value by key?

**URL:** <https://discourse.gohugo.io/t/is-it-possible-to-change-map-value-by-key/32538>\
**Category:** support\
**Created:** [April 26, 2021, 12:08pm UTC](https://discourse.gohugo.io/t/is-it-possible-to-change-map-value-by-key/32538 "2021-04-26T12:08:08Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![fairplay](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/fairplay/32/9884_2.png) [@fairplay](https://discourse.gohugo.io/u/fairplay)\
**Post date:** [April 26, 2021, 12:08pm UTC](https://discourse.gohugo.io/t/is-it-possible-to-change-map-value-by-key/32538/1 "2021-04-26T12:08:08Z")

</div>

Unexpectedly got error while trying assign/redefine map value:

```
{{ $map := dict "sort" "b" "name" "tag-b" "permalink" "/tags/tag-b/" }} 
{{ $map.sort = "a" }}

```

produces `unexpected "=" in operand` and so does try with “:=”  
Are maps in Hugo read only or there is a secret way to change value by key?

---

<div class="post-metadata">

**Author:** ![bep](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/bep/32/3332_2.png) [@bep](https://discourse.gohugo.io/u/bep)\
**Post date:** [April 26, 2021, 12:58pm UTC](https://discourse.gohugo.io/t/is-it-possible-to-change-map-value-by-key/32538/2 "2021-04-26T12:58:13Z")

</div>

> [@fairplay](#):
>
> Are maps in Hugo read only or there is a secret way to change value by key?

The only way is to use Scratch, e.g.:

```auto
{{ $s := newScratch }}
{{ $s.Set "foo" "bar" }}
{{ $map := $s.Values }}

```

---

<div class="post-metadata">

**Author:** ![fairplay](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/fairplay/32/9884_2.png) [@fairplay](https://discourse.gohugo.io/u/fairplay)\
**Post date:** [April 27, 2021, 5:05am UTC](https://discourse.gohugo.io/t/is-it-possible-to-change-map-value-by-key/32538/4 "2021-04-27T05:05:41Z")

</div>

Thank you. This method works with single map, but when it comes to my task - maps joined in slice - got same error even using Scratch:

```auto
{{ $tags := slice
    (dict "sort" "b" "name" "tag-b" "name" "Tag B" "permalink" "/tags/tag-b/" )
    (dict "sort" "c" "name" "tag-c" "name" "Tag C" "permalink" "/tags/tag-c/" )
    (dict "sort" "a" "name" "tag-a" "name" "Tag A" "permalink" "/tags/tag-a/" )
}}

{{ $s := newScratch }}
{{ $s.Set "sort" "d" }}
{{ (index $tags 0) := $s.Values }}

```

Again got the same error `unexpected ":=" in operand`

Sorry for silly question, but I don’t undestand Scratch at all. For me it looks like artifical crutch to solve problems not existing in Python, JS, PHP and other languages I usually practice.

---

<div class="post-metadata">

**Author:** ![bep](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/bep/32/3332_2.png) [@bep](https://discourse.gohugo.io/u/bep)\
**Post date:** [April 27, 2021, 6:17am UTC](https://discourse.gohugo.io/t/is-it-possible-to-change-map-value-by-key/32538/5 "2021-04-27T06:17:53Z")

</div>

> [@fairplay](#):
>
> `{{ (index $tags 0) := $s.Values }}`

Try:

{{ $tags = $tags | append $s.Values }}

---

<div class="post-metadata">

**Author:** ![fairplay](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/fairplay/32/9884_2.png) [@fairplay](https://discourse.gohugo.io/u/fairplay)\
**Post date:** [April 27, 2021, 6:29am UTC](https://discourse.gohugo.io/t/is-it-possible-to-change-map-value-by-key/32538/6 "2021-04-27T06:29:40Z")

</div>

Nope. This operation not changed target map element (first element in my case), just appended one more element:

```auto
[
map[name:Tag B permalink:/tags/tag-b/ sort:b]
map[name:Tag C permalink:/tags/tag-c/ sort:c]
map[name:Tag A permalink:/tags/tag-a/ sort:a]
map[sort:d]
]

```

---

<div class="post-metadata">

**Author:** ![chrillek](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/chrillek/32/19797_2.png) [@chrillek](https://discourse.gohugo.io/u/chrillek)\
**Post date:** [April 27, 2021, 9:55am UTC](https://discourse.gohugo.io/t/is-it-possible-to-change-map-value-by-key/32538/7 "2021-04-27T09:55:40Z")

</div>

I suppose that (like in many other languages) you can’t assign to an expression. And since there’s no way to get a _reference_ to an array element, you’re probably out of luck here.

Maybe it helps to describe what you want to _achieve_ in more abstract terms to find another solution?

---

<div class="post-metadata">

**Author:** ![fairplay](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/fairplay/32/9884_2.png) [@fairplay](https://discourse.gohugo.io/u/fairplay)\
**Post date:** [April 27, 2021, 10:38am UTC](https://discourse.gohugo.io/t/is-it-possible-to-change-map-value-by-key/32538/8 "2021-04-27T10:38:58Z")

</div>

The matter is not about “assign to expression”, but to modify existing non-const data structure. The basic instrumentary of any common language. Working example in javascript:

```auto
let map = [
{
    "displayName": "Tag B",
    "name": "tag-b",
    "permalink": "/tags/tag-b/",
    "sortCriteria": "b"
},
{
    "displayName": "Tag C",
    "name": "tag-c",
    "permalink": "/tags/tag-c/",
    "sortCriteria": "c"
},
{
    "displayName": "Tag A",
    "name": "tag-a",
    "permalink": "/tags/tag-a/",
    "sortCriteria": "a"
}
];

map[0]["sortCriteria"] = "a";

```

I’m just wondering about missing such feature in Hugo. And researching for workaround.

---

<div class="post-metadata">

**Author:** ![chrillek](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/chrillek/32/19797_2.png) [@chrillek](https://discourse.gohugo.io/u/chrillek)\
**Post date:** [April 27, 2021, 10:46am UTC](https://discourse.gohugo.io/t/is-it-possible-to-change-map-value-by-key/32538/9 "2021-04-27T10:46:13Z")

</div>

Thanks, but I knew that. I just pointed out what you were doing in your Hugo code. In my experience, it is quite pointless to compare languages, and I’m sure that Go itself permits what you want. But here we’re talking about a template language, which is apparently lacking features in comparison to others.  
That’s why I was asking about your _goal_ and musing if it might be possible to achieve it in a way that’s actually possible in Hugo.

---

<div class="post-metadata">

**Author:** ![fairplay](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/fairplay/32/9884_2.png) [@fairplay](https://discourse.gohugo.io/u/fairplay)\
**Post date:** [April 27, 2021, 11:06am UTC](https://discourse.gohugo.io/t/is-it-possible-to-change-map-value-by-key/32538/10 "2021-04-27T11:06:03Z")

</div>

Of course Golang permits maps updating, here is example from the net

```auto
package main
 
import "fmt"
 
func main() {
	var employee = map[string]int{"Mark": 10, "Sandy": 20}
	fmt.Println(employee) // Initial Map
 
	employee["Mark"] = 50 // Edit item
	fmt.Println(employee)
}

```

So I’m far away of comparing languages, just looking for instrumentary that allows me to reach the goal - process some custom structured data. [My previous topic here](https://discourse.gohugo.io/t/creating-and-sorting-slice-of-slices-is-it-possible/32513) referring to the same goal.

---

<div class="post-metadata">

**Author:** ![chrillek](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/chrillek/32/19797_2.png) [@chrillek](https://discourse.gohugo.io/u/chrillek)\
**Post date:** [April 27, 2021, 11:11am UTC](https://discourse.gohugo.io/t/is-it-possible-to-change-map-value-by-key/32538/11 "2021-04-27T11:11:49Z")

</div>

> [@chrillek](#):
>
> and I’m sure that Go itself permits what you want.

Thanks for confirming that.

---

<div class="post-metadata">

**Author:** ![jmooring](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/jmooring/32/4214_2.png) [@jmooring](https://discourse.gohugo.io/u/jmooring)\
**Post date:** [April 27, 2021, 6:26pm UTC](https://discourse.gohugo.io/t/is-it-possible-to-change-map-value-by-key/32538/12 "2021-04-27T18:26:17Z")

</div>

```nohighlight
{{ $tags := slice
  (dict "sort" "b" "name" "tag-b" "permalink" "/tags/tag-b/" )
  (dict "sort" "c" "name" "tag-c" "permalink" "/tags/tag-c/" )
  (dict "sort" "a" "name" "tag-a" "permalink" "/tags/tag-a/" )
}}

{{ $sliceIndex := 0 }} {{/* Which item in the slice do you wish to modify? */}}
{{ $mapKey := "sort" }} {{/* Which key do you wish to modify? */}}
{{ $newValue := "z" }} {{/* What's the new value? */}}

{{ $s := newScratch }}
{{ range seq 0 (sub (len $tags) 1) }}
  {{ $s.Set (printf "%04d" .) (index $tags .) }}
{{ end }}
{{ $s.SetInMap (printf "%04d" $sliceIndex) $mapKey $newValue }}

{{/* Here's what the map looks like. */}}
<pre>{{ jsonify (dict "indent" " ") $s.Values }}</pre>

{{/* Now convert the map back to a slice of maps. */}}
{{ $tags := slice }}
{{ range $s.Values }}
  {{ $tags = $tags | append . }}
{{ end }}

{{/* Here's what the slice looks like. */}}
<pre>{{ jsonify (dict "indent" " ") $tags }}</pre>

```

---

<div class="post-metadata">

**Author:** ![jmooring](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/jmooring/32/4214_2.png) [@jmooring](https://discourse.gohugo.io/u/jmooring)\
**Post date:** [April 27, 2021, 6:37pm UTC](https://discourse.gohugo.io/t/is-it-possible-to-change-map-value-by-key/32538/13 "2021-04-27T18:37:13Z")

</div>

Some time ago I created a partial to perform slice manipulation (insert, replace, delete). Something like:

```auto
{{ $newSlice := partial "modify-slice" (dict "slice" $tags "action"="replace" "index" 2 "item" $newItem }}

```

This would only save you three lines of code in the example above (the last bit, where we convert the map back to a slice of maps).

---

<div class="post-metadata">

**Author:** ![fairplay](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/fairplay/32/9884_2.png) [@fairplay](https://discourse.gohugo.io/u/fairplay)\
**Post date:** [April 28, 2021, 5:45pm UTC](https://discourse.gohugo.io/t/is-it-possible-to-change-map-value-by-key/32538/14 "2021-04-28T17:45:20Z")

</div>

Thank you! Such way of data manipulation is a new dimension in my Hugo experience.

Regarding initial topic. Significant benefit of static site generators is that excessive and not speed/data/size optimized code runs only on local machine and not taking much more valuable web server resources. So I’ve already launch a straightforward (and not too elegant) workaround solution via intermediate array. But definitely will test your solution.

---

<div class="post-metadata">

**Author:** ![system](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/system/32/1_2.png) [@system](https://discourse.gohugo.io/u/system)\
**Post date:** [April 30, 2021, 5:46pm UTC](https://discourse.gohugo.io/t/is-it-possible-to-change-map-value-by-key/32538/15 "2021-04-30T17:46:06Z")

</div>

Closed on April 30, 2021, 5:46pm UTC

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