matt1
Merging 2 nested maps
Hello guys, I have a maybe dumb question but I’m trying without success to merge 2 maps with nested values
%{"XX" => %{"A" => 1}} %{"XX" => %{"B" => 3}, "YY" => %{"A" => 2}} and the expected result is
%{"XX" => %{"A" => 1, "B" => 3}, "YY" => %{"A" => 2}}
but map1 |> Map.merge(map2) only merge the root keys
any idea?
Marked As Solved
NobbZ
You need to use Map.merge/3 and provide a conflict handler that handles the conflicts recursively.
4
Also Liked
matt1
axelson
Scenic Core Team
GreenLoofa
I was looking for information on maps that might have more nesting and couldn’t find anything. I came up with this and hope it helps others also searching for that:
def merge_nested(map1, map2) do
Map.merge(map1, map2, &merge_nested/3)
end
def merge_nested(_k, %{} = v1, %{} = v2) do
Map.merge(v1, v2, &merge_nested/3)
end
def merge_nested(_k, _v1, v2), do: v2
For example:
a = %{a: %{b: %{c: 1}}}
b = %{a: %{b: %{d: 2}}}
merge_nested(a, b)
#> %{a: %{b: %{c: 1, d: 2}}}
1
Last Post!
axelson
Scenic Core Team
Popular in Questions
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
New
Hi guys, i’m new in the Elixir world, and i have to say, that i love it!
i’m having some problem to understand anonymous functions with ...
New
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? Ecto.Repo — Ecto v3.14.0 has exampl...
New
Hi everyone,
I was playing with phoenix liveView but I run into an issue. I have a form and want to validate each input text when the te...
New
As the title describes, I’m trying to run Enum.map() over a list of key/value pairs, where the value is a map. My data looks like this:
...
New
Hi,
I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
New
I am trying to run a deploy with docker and I successfully runned with this command:
docker build -t romenigld/blog-prod .
but when I t...
New
Other popular topics
Hi All,
I set a environment variables in dev.exs , like below code.
when i start server, how can i set the ${enable} value?
thanks.
d...
New
This post is a wiki (feel free to hit the edit button near the bottom right of this post to add your own changes!)
This post collects co...
New
i’m a new one to elixir
which editor can i use
vs code? or atom?
Thanks! :smiley:
New
I am trying to run a deploy with docker and I successfully runned with this command:
docker build -t romenigld/blog-prod .
but when I t...
New
This post is an instruction guide to help you setup your Neovim for Elixir development from scratch. It includes general information on h...
New
In the code below, if the create action is not set to accept “extra_key” as an input, it errors out with a message shown above. Is there ...
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #supervisor
- #elixirconf-us
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #metaprogramming
- #security
- #hex










