Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Updating a nested map in Elixir

Tags:

elixir

I have a 2-level-nested map, how can I update each value on the 2nd level? Right now I'm doing this:

  items = Enum.map(items, fn(a) ->
    a.items2 = Enum.map(a.items2, fn(a2) ->
      Map.put(x2, :some_key, 123) 
    end)

    a
  end)

An error:

cannot invoke remote function "a.items2/0" inside match.

I basically know what this means, but how to fix it?

Note that a.items2 might also has a nested map in itself.

like image 718
Oumak81 Avatar asked Jul 15 '26 20:07

Oumak81


1 Answers

Enum.map(items, fn({k,v}) ->
  {k, put_in(v, [:items2, :some_key], 123)}    
end)
like image 138
Scott Thompson Avatar answered Jul 19 '26 09:07

Scott Thompson



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!