Let's assume that we have the following:
list =
[
%{"id" => 1, "name" => "Alice"},
%{"id" => 2, "name" => "Bob"},
%{"id" => 3, "name" => "Charlie"}
]
In order to add key type
with a value person
to each element we could do:
list |> Enum.map(fn x -> Map.put(x, "type", "person") end)
which will result in:
[
%{"id" => 1, "name" => "Alice", "type" => "person"},
%{"id" => 3, "name" => "Bob", "type" => "person"},
%{"id" => 3, "name" => "Charlie", "type" => "person"}
]
Now, let's say that we have another list:
types = ["human", "alien", "unknown"]
How to combine list
and types
so that the end result will be:
[
%{"id" => 1, "name" => "Alice", "type" => "human"},
%{"id" => 2, "name" => "Bob", "type" => "alien"},
%{"id" => 2, "name" => "Charlie", "type" => "unknown"}
]
?