Add element to map in a loop

I have yaml datafiles in a folder and I would like to use them in a page. The structure is something like this one below.

    - apple: 1
      pear: 2
      plum: 3
    - apple: 4
      pear: 5
      plum: 6

I want to read all of them and merge the things lists into one (so far I’ve done it) and I also want to attach a new key-value for every one of them in the process.

I have the following code, where $data is the name of the yaml file from the outer loop.

    {{ $.Scratch.Add "things"  (slice) }}
    {{ range index .Site.Data.stuff $data "things" }}
    {{ $.Scratch.Set "temp" . }}
    {{ $.Scratch.SetInMap "temp" "foobar" 1 }}
    {{ $.Scratch.Add "things" ($.Scratch.Get "temp")   }}
    {{ end }}

I get the following error: “interface conversion: interface {} is map[interface {}]interface {}, not map[string]interface {}”

If I comment out the SetInMap line it works. Is this possible at all?
Thanks in advance

Edit: I do not want to add this property to every element in every data file.

I rephrased my logic so this does not blocks me anymore, but I am still curious whether such thing makes any sense at all and possible in Hugo.

I don’t understand why it doesn’t work, but I could reproduce the issue here using your YAML sample:

{{ $list := index .Site.Data.items "things" }}
{{ range $item := $list }}
    {{ $.Scratch.Set "items" $item }}
    {{ $.Scratch.SetInMap "items" "foobar" "1" }}
{{ end }}

However, changing the first line makes it work:

{{ $list := slice (dict "apple" 1 "pear" 2 "plum" 3) (dict "apple" 4 "pear" 5 "plum" 6) }}

Have you tried reproducing the issue with different formats than YAML?

Apart from this, you can alter your logic to loop through the actual items with a second loop. This works:

{{/* Initialize "results" so it does not hold old values */}}
{{ $.Scratch.Set "results" (dict "apple" 0) }}
{{ range index .Site.Data.items "things" }}
    {{ range $i, $j := . }}
        {{ $x := $.Scratch.Get "results" }}
        {{ if isset $x $i }}
            {{/* This item is already initialized, we can add! */}}
            {{ $.Scratch.SetInMap "results" $i (add $j (index $x $i)) }}
        {{ else }}
            {{ $.Scratch.SetInMap "results" $i $j }}
        {{ end }}
    {{ end }}
{{ end }}
{{ $.Scratch.Get "results" }}