How to access all values in a list of map using an identifier?

I have a list of maps containing different values

list =
[
 %Samp1{
 id: "11111",
 sample_data1: "",
 sample_data2: "",
 other_datas: {}
 },
 %Samp2{
 id: "22222",
 sample_data1: "",
 sample_data2: "",
 other_datas: {}
 },
 %Samp3{
 id: "33333",
 sample_data1: "",
 sample_data2: "",
 other_datas: {}
 },
]

How can i get all the values along with the ID i want to match, For Example:
if id == “33333” i want to traverse and get all values with it, so the expected result should be:

%Samp3{
 id: "33333",
 sample_data1: "",
 sample_data2: "",
 other_datas: {}
 },

I tried Enum.map but I can only fetch single value each

list |> Enum.map(& &1.id)

Result: ["11111", "22222", "33333"]

Hi @skedaddl3 the function you’re looking for is Enum — Elixir v1.14.2 whoops! I mean Enum — Elixir v1.14.2

2 Likes

Enum.find will stop after first match… probably for comprehension, with filter on id would do the trick.

1 Like
Enum.filter(list, & &1.id == id)

Or did I misunderstand? :slight_smile:

1 Like

Thanks! Enum.filter is what i needed :grin: