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
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
What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.
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
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
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
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
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
Elixir plugin for JetBrain’s IntelliJ Platform (including Rubymine)
This is a plugin that adds support for Elixir to JetBrains IntelliJ...
New
Original source of discussion: This topic on the Pragmatic Programmers’ Functional Web Development with Elixir, OTP, and Phoenix forum.
...
New
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...
New
Seen any cool LiveView demos, sample apps or examples? Please post them here! :003:
New
Hello!
tl;dr Announcing Oban, an Ecto based job processing library with a focus on reliability and historical observability.
After spen...
New
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
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









