t.t
Trying to access a key inside a map
i want to access the “defaultValue” key.
%{
"category" => {:ok,
%{
"defaultValue" => 10,
"detailed" => [
%{"categoryName" => "test", "categoryValue" => 23}
],
"total" => 1
}}
}
iex(56)> test_map["category"]
{:ok,
%{
"defaultValue" => 10,
"detailed" => [%{"categoryName" => "test", "categoryValue" => 23}],
"total" => 1
}}
normally, i’d do this: test_map["category"]["defaultValue"]
but i get this error in return:
iex(57)> test_map["category"]["defaultValue"]
** (FunctionClauseError) no function clause matching in Access.get/3
The following arguments were given to Access.get/3:
# 1
{:ok,
%{
"defaultValue" => 10,
"detailed" => [%{"categoryName" => "test", "categoryValue" => 23}],
"total" => 1
}}
# 2
"defaultValue"
# 3
nil
Attempted function clauses (showing 5 out of 5):
def get(%module{} = container, key, default)
def get(map, key, default) when is_map(map)
def get(list, key, default) when is_list(list) and is_atom(key)
def get(list, key, _default) when is_list(list)
def get(nil, _key, default)
(elixir) lib/access.ex:265: Access.get/3
i noticed i have a tuple with a atom :ok and a map inside it, and that’s probably why i can’t access my “defaultValue” key but i’m new in elixir/functional programming and i am having a hard time trying to find the solution.
Marked As Solved
kokolegorille
Yes You can…
I would say FP is made of pipeline of transformation.
Enum.reduce test_map, %{}, fn {k, {:ok, v}}, acc -> Map.put(acc, k, v) end
Also Liked
kokolegorille
Hello and welcome,
iex(1)> elem(test_map["category"], 1)["defaultValue"]
10
BTW it is kind of ugly to have this ok tuple inside, and I would think of reshaping the data first.
1
emoragaf
I totally missed the tuple, but you can use the get_in ability to pass functions to handle the tuples
map = %{
"category" => {:ok,
%{
"defaultValue" => 10,
"detailed" => [
%{"categoryName" => "test", "categoryValue" => 23}
],
"total" => 1
}}
}
cleaned_map = %{
"category" =>%{
"defaultValue" => 10,
"detailed" => [
%{"categoryName" => "test", "categoryValue" => 23}
],
"total" => 1
}
}
# get the value if its a tuple
val = fn :get, {:ok, v}, next -> next.(v)
:get, data, next -> next.(data) end
# It works on the original map
get_in(map, ["category", val, "defaultValue"])
|> IO.inspect
# 10
# Also works on a preprocessed map
get_in(cleaned_map, ["category", val, "defaultValue"])
|> IO.inspect
# 10
1
Popular in Questions
Hi,
I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list. ...
New
Okay, I’m having a heck of a time trying to figure out how to best handle the validation of belongs_to associations in Ecto. I’m sure I’...
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
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
I am constructing a JSON object (map) and I need to conditionally set a field. I’m trying to write proper elixir-way code… and I’m at a l...
New
Using vs code and installed ElixirLS: support and debugger.
And I got an error popped up on start up says
Failed to run ‘elixir’ comma...
New
Hi all,
Trying to get some more clarity over utc_datetime and naive_datetime for Ecto:
The documentation above suggests that while ...
New
i’m a new one to elixir
which editor can i use
vs code? or atom?
Thanks! :smiley:
New
I’m trying to make a websocket server in Phoenix or raw Elixir. I heard about gun, I think I could use cowboy, but since I’m not that sma...
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
Other popular topics
I’ve read in another post that it may be possible with a router helper - but I couldn’t find an appropriate one, and tbh, I’m still just ...
New
Hi folks,
Few months ago I have announced the proof-of-concept of the library to manipulate the browsers DOM objects directly from Elixi...
New
Hey all,
I discovered Elixir and I love it. I always wanted to learn a functional programming and I intended to go for Haskell, but afte...
New
I am going through the kafka architecture. All the features what the kafka is providing are already in Erlang. I would like hear your opi...
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
Good day to you all.
I have been struggling to get a query involving like and ilike to work.
Can anyone assist me on this, please?
pro...
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
Kind of like when jquery came out, it was super necessary. Existing drag and drop libraries have a bunch of baggage to support old browse...
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
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
- #advent-of-code
- #elixirconf-us
- #distillery
- #processes
- #forms
- #api
- #metaprogramming
- #security
- #performance








