Get a value from struct inside list

I have a list as follows which has

    url: "http://localhost:4000/test/img",
    user: "localhost:4000/test/img/123"}]

How do I get the result as "localhost:4000/test/img/123" which is a string?

Can anyone help me with this?

Thanks in advance.

Answers 2

  • If the struct is under your control, you might implement Access for it and use get_in/2

    get_in(list, [Access.all(), :url]

    Also, the comprehension and pattern matching work well.

    for %Photos{url: url} <- list, do: url

    The last, but not the least way would be

    list |> &1.url)

    All the above return all urls in the list. To get to the specific one, use and/or pattern matching.

Related Articles