How to concatenate Map in same level?

I want to concatenate three or more maps in a same hierarchy in a loop.
I am having one loop which returns me a map , i want to show like

{
 {loop1: "some val"},
 {loop2: "some val"},
 {loop3: "some val"}
}

If i use compose its coming like

{
{
 {loop1: "some val"},
 {loop2: "some val"}
},
 {loop3: "some val"}
}

It depends on what you are doing. Please show the code using some tool. I think it’ll be easier to understand your problem.

1 Like

You mean like this?

$ elixir demo.exs
%{
  key64: %{key41: %{key1: "value1"}, key42: %{key2: "value2"}},
  key65: %{key53: %{key3: "value3"}}
}
%{key1: "value1", key2: "value2", key3: "value3"}
$
# file: demo.exs
defmodule Demo do
  # ...
end

map1 = %{key1: "value1"}
map2 = %{key2: "value2"}
map3 = %{key3: "value3"}
map4 = %{key41: map1, key42: map2}
map5 = %{key53: map3}
map6 = %{key64: map4, key65: map5}

IO.inspect(map6)
map7 =
  map6
  |> Demo.to_list()
  |> Map.new()
IO.inspect(map7)
1 Like

Enum.reduce
https://hexdocs.pm/elixir/Enum.html#reduce/3

1 Like

@psprakashkkdi This can help you a bit

Eg. if your Key-Value pairs are in this format (in your example you have given tuple, but I guess this should be list like specified below):

list = [[ %{loop1: "val"}, %{loop2: "val"}],%{loop3: "val"}]
iex(1)> Enum.concat(list)
[%{loop1: "val"}, %{loop2: "val"}, %{loop3: "val"}]

I hope this is what you are looking for…

2 Likes

Thanks @pedromvieira i got the solution.I have went through the documentation.

Thanks @peerreynders for the help.Clear explanation.

Thanks @pmangalakader it worked

mate @tiagodavi i got the solution.Thanks for the time.