CleanIfElse
Map inside a list. Search if Key and value exists and grab the entire map
First time posting on this forum so yay! I am new to Elixir and decided to post a question I been struggling on.
So there is a list and inside the list are maps. I am trying to grab the map which has key of “type” and the value of “ITEM_PURCHASED”. Others can be completely ignored. I used Map.take function but the problem is, it grabs the the first map below.
%{
"itemId" => 1052,
"participantId" => 8,
"timestamp" => 1152905,
"type" => "ITEM_DESTROYED"
},
%{
"itemId" => 1052,
"participantId" => 5,
"timestamp" => 1153731,
"type" => "ITEM_PURCHASED"
},
%{
"levelUpType" => "NORMAL",
"participantId" => 1,
"skillSlot" => 2,
"timestamp" => 1155348,
"type" => "SKILL_LEVEL_UP"
},
Cheers,
Cleanifelse
Most Liked
eksperimental
iex(9)> list
[
%{
"itemId" => 1052,
"participantId" => 8,
"timestamp" => 1152905,
"type" => "ITEM_DESTROYED"
},
%{
"itemId" => 1052,
"participantId" => 5,
"timestamp" => 1153731,
"type" => "ITEM_PURCHASED"
},
%{
"levelUpType" => "NORMAL",
"participantId" => 1,
"skillSlot" => 2,
"timestamp" => 1155348,
"type" => "SKILL_LEVEL_UP"
}
]
iex(10)> Enum.find(list, fn map -> map["type"] == "ITEM_PURCHASED" end)
%{
"itemId" => 1052,
"participantId" => 5,
"timestamp" => 1153731,
"type" => "ITEM_PURCHASED"
}
5
eksperimental
By the way, you shouldn’t be using strings, but atoms as keys for maps, and for “type” values as well
2
CleanIfElse
Thank you Eksperimental! I will keep that in mind as well. ![]()
2
Popular in Questions
** (DBConnection.ConnectionError) connection not available and request was dropped from queue after 2733ms. You can configure how long re...
New
In Ruby, I can go:
User.find_by(email: "foobar@email.com").update(email: "hello@email.com")
How can I do something similar in Elixir?
...
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
Hi,
I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New
I have a phoenix application from which a user can download multiple(5-6) files of size 1MB. I couldn’t find anything related to sending ...
New
Could someone help me? I’m making my first elixir program, number guessing game. I can’t figure out how to convert the user’s guess from ...
New
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New
I am trying to start a new phoenix project with elixir 1.9, but mix phx.new does not work. It says that ** (Mix) The task "phx.new" could...
New
In the Dialyzer docs ( dialyzer — OTP 29.0.2 (dialyzer 6.0.1) ), there is a way to turn off a specific warning for a function:
-dialyzer...
New
Hi everyone!
I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New
Other popular topics
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible.
total = 10
while total != 0
...
New
lets say i have a sample like
a = 20; b = 10;
if (a > b) do
{:ok, "a"}
end
if (a < b) do
{:ok, b}
end
if (a == b) do
{:ok, "equa...
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
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New
i’m a new one to elixir
which editor can i use
vs code? or atom?
Thanks! :smiley:
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
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
Update:
How to use the Blogs & Podcasts section
You can post links to your blog posts or podcasts either in one of the Official Blog...
New
Seen any cool LiveView demos, sample apps or examples? Please post them here! :003:
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









