Enum.filter not working for me

I have the following map

  qas = %{
    "0" => %{"answer" => "have an answer", "question" => "have a question"},
    "1" => %{"answer" => "", "question" => ""},
    "10" => %{"answer" => "", "question" => ""},
    "11" => %{"answer" => "ans 2", "question" => "q 2"},
    "2" => %{"answer" => "", "question" => ""},
    "3" => %{"answer" => "", "question" => ""},
    "4" => %{"answer" => "", "question" => ""},
    "5" => %{"answer" => "", "question" => ""},
    "6" => %{"answer" => "", "question" => ""},
    "7" => %{"answer" => "", "question" => ""},
    "8" => %{"answer" => "", "question" => ""},
    "9" => %{"answer" => "", "question" => ""}
  }

I want to get rid of all the empty questions

qas |>  Enum.filter(fn q -> { %{"answer" => ""}} end)

return the same value

How to get rid of all the empty questions?

When enumerating a Map, there’s a tuple with two elements (key and value) being sent as a parameter to the fun for each iteration. So in your case, you probably want to do this:

Enum.filter(qas, fn {_id, q_and_a} -> q_and_a["question"] != "" end)
2 Likes

Great. Thanks

1 Like