Making a list of map using List of entities and tuples

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.

It’s better to use for comprehension, with 2 generators, and one filter…

o = [%{id: 1, value: "blue"}, %{id: 2, value: "black"}]
v = [%{option_id: 1, votes: 1}, %{option_id: 2, votes: 2}]
for %{id: id} = x <- o, %{option_id: o_id} = y <- v, id == o_id do 
  %{id: x.id, option: x.value, votes: y.votes} 
end
[%{id: 1, option: "blue", votes: 1}, %{id: 2, option: "black", votes: 2}]

I see also that You do not use anonymous functions the right way…

It’s not

fn -> {} end

it’s

fn -> end

And the result of failed if condition is nil.

Thank you very much for the correct way to do it.
Also thank you for pointing out my mistake.