Returns a map Map
that is the result of calling Fun(Key, Value1)
for every Key
to value Value1
association in MapOrIter
in any order.
If Fun(Key, Value1)
returns true
, the association is copied to the result map. If it returns false
, the association is not copied. If it returns {true, NewValue}
, the value for Key
is replaced with NewValue
at this position is replaced in the result map.
The call fails with a {badmap,Map}
exception if MapOrIter
is not a map or valid iterator, or with badarg
if Fun
is not a function of arity 2.
Example:
> Fun = fun(K,V) when is_atom(K) -> {true, V*2}; (_,V) -> (V rem 2) =:= 0 end,
Map = #{k1 => 1, "k2" => 2, "k3" => 3},
maps:filtermap(Fun,Map).
#{k1 => 2,"k2" => 2}