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
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
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
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
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New
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
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
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
Hi!
In PHP: $_SERVER[‘SERVER_ADDR’] - in Elixir?
Searched the docs for ip address and the web, no good results.
Thanks!
New
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
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
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
I have an umbrella app.
Some of the apps inside depend on other apps in the umbrella, unsurprisingly.
I’m writing a test for one of the...
New
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
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #supervisor
- #elixirconf-us
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #metaprogramming
- #security
- #hex









