Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Merge map with array values in Elixir

Tags:

elixir

Hello i have this array

temp=[%{"a"=>1},%{"b"=>2}]

total=%{"c"=>3,"d"=>4}

how do i add temp to total so i end with

total=%{"a"=>1,"b"=>2,"c"=>3,"d"=>4}

like image 611
ramstein Avatar asked Dec 18 '22 19:12

ramstein


1 Answers

You can use total as the initial value for Enum.reduce/3 and Map.merge/2 all of the values in your list.

Enum.reduce(temp, total, fn (map, acc) -> Map.merge(acc, map) end)

Returns:

%{"a" => 1, "b" => 2, "c" => 3, "d" => 4}
like image 86
Gazler Avatar answered Dec 31 '22 19:12

Gazler