lapinkoira
lapinkoira

Reputation: 8988

Combine Enum.filter with Enum.each

With the following list of maps:

[%{test: %{"one" => 1, "three" => 3, "two" => 2}}, ...]

Shouldnt this code not filter anything at all?

Enum.filter([map], fn(number) ->    
  Enum.each(number.test, fn {k, v} -> 
    v == 4                             
  end)                               
end)

How can I make Enum.filter work with a property which is a map itself?

Upvotes: 1

Views: 228

Answers (1)

Dogbert
Dogbert

Reputation: 222148

Enum.each returns :ok so your code will always return the same value as input.

If you want to check that any value in the map has value 4, you can use Enum.any?/2:

Enum.any?(number.test, fn {k, v} -> 
  v == 4                             
end)

To check if all of them have value 4, you can use Enum.all?/2:

Enum.all?(number.test, fn {k, v} -> 
  v == 4                             
end)  

Upvotes: 2

Related Questions