How to take out a value from a list of structs

struct
enum

#1

Hey I have a list of structs and it has fields like: id, date, counter etc
how would i take out every id from this list and return the ids as a new list?


#2

Enum.map/2 is your friend here.


#3

As NobbZ said, something like:

Enum.map(my_structs, fn %{id: id} -> id end)

#4

I ended up doing this
Enum.map(x, fn y -> y.user_event.user_id end)


#5

You might use the short version, like this

Enum.map(x, & &1.user_event.user_id)

#6

love it thanks