How can I convert this list in elixir
[
%{key1: 1, key2: 2, key3: 3},
%{key1: 4, key2: 5, key3: 6},
%{key1: 7, key2: 8, key3: 9}
]
to this map
%{key1: [1, 4, 7], key2: [2, 5, 8], key3: [3, 6, 9])
?
Enum.reduce(input, %{key1: [], key2: [], key3: []}, fn m, acc ->
Map.merge(acc, m, fn _k, v1, v2 -> v1 ++ [v2] end)
end)
#⇒ %{key1: [1, 4, 7], key2: [2, 5, 8], key3: [3, 6, 9]}
or, without preliminar hardcoded accumulator:
Enum.reduce(input, %{}, fn m, acc ->
Map.merge(acc, m, fn
_k, v1, v2 when is_list(v1) ->
:lists.reverse([v2 | :lists.reverse(v1)])
_k, v1, v2 -> [v1, v2]
end)
end)
#⇒ %{key1: [1, 4, 7], key2: [2, 5, 8], key3: [3, 6, 9]}
You can do this in two passes; The first flattens everything into a single one-dimensional list of key-value pairs, then the second pass groups by the keys.
input = [
%{key1: 1, key2: 2, key3: 3},
%{key1: 4, key2: 5, key3: 6},
%{key1: 7, key2: 8, key3: 9}
]
input
|> Enum.flat_map(fn m -> Map.to_list(m) end)
|> Enum.group_by(fn {k, _} -> k end, fn {_, v} -> v end)
Output
%{key1: [1, 4, 7], key2: [2, 5, 8], key3: [3, 6, 9]}