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

itssasanka
Hi all, Trying to get some more clarity over utc_datetime and naive_datetime for Ecto: https://hexdocs.pm/ecto/Ecto.Schema.html#module-...
New
sacepums
Hey guys. I'm new to elixir and im really stocked about it. But I ran into a bit of problem - I need to convert a date sting, for examp...
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
mathew4509
I have a list say x = ["23gh", "56kh", "97mh"] I would like to pass each element to Val in each iteration. Say, in iteration 1 -------...
New
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New
beno
I will often find my self writing things similar to: case some_value do nil -> something() "" -> something() _ -> someth...
New
Fl4m3Ph03n1x
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
jc00ke
Expanding on this topic: https://forum.elixirforum.net/t/map-typespec-question/19217 Let’s say I have a map with required and optional k...
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
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

Other popular topics Top

senggen
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
William
I would like to know that is there any online source for learning Phoenix Framework for building E-Commerce Store? Any advantage on build...
New
lessless
I believe there are people here who are dealing with CSV files import on the daily basis, and since Excel is a really popular tool there ...
New
Jim
As a follow up to my earlier question: I have the code compiling and running but not getting a successful login from the rest server. ...
New
vertexbuffer
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
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
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
New
stefanluptak
Hello everybody, usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
New
romenigld
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
siddhant3030
Hi, I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
New

We're in Beta

About us Mission Statement