CleanIfElse

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

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"
}
eksperimental

eksperimental

By the way, you shouldn’t be using strings, but atoms as keys for maps, and for “type” values as well

CleanIfElse

CleanIfElse

Thank you Eksperimental! I will keep that in mind as well. :slightly_smiling_face:

Where Next?

Popular in Questions Top

chokchit
** (DBConnection.ConnectionError) connection not available and request was dropped from queue after 2733ms. You can configure how long re...
New
sergio
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
Darmani72
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
electic
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
greenz1
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
mgjohns61585
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
sergio_101
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
shijith.k
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
yawaramin
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
lanycrost
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 Top

aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
New
hariharasudhan94
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
minhajuddin
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
stefanchrobot
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
joeerl
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
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
saif
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
joaquinalcerro
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
Qqwy
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...
3271 127089 1222
New
AstonJ
Seen any cool LiveView demos, sample apps or examples? Please post them here! :003:
New

Latest on Elixir Forum

Elixir Forum

We're in Beta

About us Mission Statement