Removing empty values in a nested map

I have a nested map as such:

%{product: %{category: "45", code: nil}, date: "2021-04-01"}

I am trying to remove all items that do have an empty value.
I have tried it as follows:

def removeEmpty(%{} = map) do
    Enum.into(
      Enum.reject(map, fn {_k, v} ->
        if is_map(v) do
          removeEmpty(v)
        end

        is_nil(v)
      end),
      %{}
    )
  end

Alas my unit test fails because the result still has the empty value in it:

Assertion with == failed
     code:  assert result == %{product: %{category: "45"}, date: "2021-04-01"}
     left:  %{date: "2021-04-01", product: %{category: "45", code: nil}}
     right: %{date: "2021-04-01", product: %{category: "45"}}
     stacktrace:
       test/map_helper_test.exs:23: (test)

It works for single level maps, but I need help on making this work for multi-level maps.

One issue is that here the result of removeEmpty(v) is discarded. Data is immutable in Elixir, so that call would return a new value, not mutate the value v. But you don’t capture the new value, so it is lost.

You might try something like this instead at the simplest:

v = if is_map(v) do
  removeEmpty(v)
else
  v
end
1 Like

You have to write it as a transformation. If you use only a filter, it will of course doesn’t change the value if it’s a map.

def removeEmpty(%{} = map) do  
  for {k,v} <- map, !is_nil(v), into: %{}, do: {k, removeEmpty(v)}
end
def removeEmpty(v), do: v
1 Like