I’m trying to figure how to achieve the output below.
[
%{"id" => 2, "option" => "blue", "votes" => 2},
%{"id" => 3, "option" => "black", "votes" => 1},
%{"id" => 1, "option" => "red", "votes" => 3}
]
but what I tried was partially correct but there are extra values added.
iex(60)> Enum.map(o, fn o1 -> {
...(60)> Enum.map(v, fn v1 -> {
...(60)> if (o1.id == v1.option_id) do
...(60)> %{"id" => o1.id, "option" => o1.value, "votes" => v1.votes}
...(60)> end
...(60)> } end )
...(60)> } end )
[
{[{nil}, {nil}, {%{"id" => 1, "option" => "1", "votes" => 3}}]},
{[{nil}, {%{"id" => 2, "option" => "2", "votes" => 2}}, {nil}]},
{[{%{"id" => 3, "option" => "3", "votes" => 1}}, {nil}, {nil}]}
]
where o is
[
%Pickr.Polls.Option{
...
id: 1,
value: "red",
...
},
%Pickr.Polls.Option{
...
id: 2,
value: "blue",
...
},
%Pickr.Polls.Option{
...
id: 3,
value: "black",
...
}
]
and v is
[
%{option_id: 3, votes: 1},
%{option_id: 2, votes: 2},
%{option_id: 1, votes: 3}
]
How could I achieve the expected result.
Thank you very much.