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
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
3
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
1
Popular in Questions
What is the proper way to load a module from a file in to IEX?
In the python world, doing something like this pretty standard:
from ....
New
I have followed this StackOverflow post to install the specific version of Erlang.
And When I am running mix ecto.setup then getting fol...
New
Hey guys.
I'm new to elixir and im really stocked about it. But I ran into a bit of problem - I need to convert a date sting, for examp...
New
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible.
total = 10
while total != 0
...
New
Hey,
Just curious what are the main benefits of Elixir compared to Clojure?
When is Elixir more useful than Clojure and vice versa?
Th...
New
I’m currently trying to understand how to join three tables using Ecto. All the examples I’ve seen use 2, so maybe I’m just missing somet...
New
Hi all
I want to have a unix time, from the current time plus 1 hour.
DateTime.now + 1 hour
How to get it in elixir?
Thanks
New
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
Can someone explain the settings of pool_size of Ecto in config file? and what is the recommend size?
Thanks
New
Hi,
I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
New
Other popular topics
Manning 2016 Halloween weekend sale via Deal of the Day
Friday, October 28 - Half off all MEAPs - code WM102816LT
Saturday, October 29 ...
New
Hello!
Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
New
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 record...
New
This post is a wiki (feel free to hit the edit button near the bottom right of this post to add your own changes!)
This post collects co...
New
I want to try my hand at web scraping. What tools/libraries do I need to use. I’m hoping to turn this into something professional so don’...
New
As a follow up to my earlier question:
I have the code compiling and running but not getting a successful login from the rest server. ...
New
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
Is there a way to get the call stack or stack trace at any point in the code? Not from exceptions, but an expression that returns how the...
New
Hello, I have map which I want to convert it to string like this:
the map:
%{last_name: "tavakkoli", name: "shahryar"}
the string I ne...
New







