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

vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
mcarvalho
What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.
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
sen
Hi All, I set a environment variables in dev.exs , like below code. when i start server, how can i set the ${enable} value? thanks. d...
New
fayddelight
I tried installing elixir 1.11.2 erlang 23.3.4 via asdf in my zsh shell. Enabled the versions locally and globally. When I list them ...
New
Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
New
senggen
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] 15:22:35.803 [error] gen_event {lager_file_backend...
New

Other popular topics Top

KronicDeth
Elixir plugin for JetBrain’s IntelliJ Platform (including Rubymine) This is a plugin that adds support for Elixir to JetBrains IntelliJ...
289 36689 110
New
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers’ Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New
gausby
I asked this very same question on twitter and got some interesting feedback, but I thought it would be a good question to ask here as we...
1207 40082 209
New
AstonJ
Seen any cool LiveView demos, sample apps or examples? Please post them here! :003:
New
sorentwo
Hello! tl;dr Announcing Oban, an Ecto based job processing library with a focus on reliability and historical observability. After spen...
985 44608 311
New
saif
Hello everyone, Long time lurker first time poster here. I’ve recently begun working on Elixir full-time again! :raised_hands: It’s been...
New

We're in Beta

About us Mission Statement