eddy147

eddy147

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.

Most Liked

pichi

pichi

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
Nicd

Nicd

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

Where Next?

Popular in Questions Top

Emily
I have VueJS GUIs with the project generated using Webpack. I have Elixir modules that will need to be used by the VueJS GUIs. I forese...
New
Darmani72
If I have a post route which an argument: post /my_post_route/:my_param1, MyController.my_post_handler How would get the post params ...
New
Fl4m3Ph03n1x
About me? ( if you have nothing better to do than reading about some random guy in the internet :stuck_out_tongue: ) Hello all, this is ...
New
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New
fireproofsocks
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? Ecto.Repo — Ecto v3.14.0 has exampl...
New
komlanvi
Hi everyone, I was playing with phoenix liveView but I run into an issue. I have a form and want to validate each input text when the te...
New
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New

Other popular topics Top

nobody
Hi! In PHP: $_SERVER[‘SERVER_ADDR’] - in Elixir? Searched the docs for ip address and the web, no good results. Thanks!
New
minhajuddin
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
New
vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
joaquinalcerro
Hi there, I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 records...
New
New
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
New

We're in Beta

About us Mission Statement