matt1

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

NobbZ

You need to use Map.merge/3 and provide a conflict handler that handles the conflicts recursively.

Also Liked

matt1

matt1

Awesome

solved with

 result = ma1 |> Map.merge(map2, fn _k, v1, v2 ->
      v1 |> Map.merge(v2)
    end)

ty so much @NobbZ !!

axelson

axelson

Scenic Core Team
GreenLoofa

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}}}

Last Post!

axelson

axelson

Scenic Core Team

Where Next?

Popular in Questions Top

minhajuddin
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
baxterw3b
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
fireproofsocks
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
komlanvi
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
aalberti333
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
siddhant3030
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
romenigld
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 Top

sen
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
axelson
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...
239 49134 226
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
romenigld
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
TunkShif
This post is an instruction guide to help you setup your Neovim for Elixir development from scratch. It includes general information on h...
274 42576 114
New
jason.o
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