Create a new map with multiple keys through each iteration of

Hello all,

I have a list of structs, and I want to iterate through each element and from each element create a new map with multiple keys.
Like the example bellow:

User.list_users() // Returns a list of User structs
|> user ->
      {"#{user.first_name} #{user.last_name}",
         "id" =>,
         "role" => user.role           

But what I actually want to return for Phoenix View, is a list with maps like this:

[%{"id" =>, "role" => user.role, "name" =>}, {"id" =>, "role" => user.role, "name" =>}, ...]

So this way Phoenix view returns a list of objects like that.
I’m trying to find a solution for this but only allows for one key, value pair.
Does anyone know how I could solve this?

Thanks in advance for all the help!

Hi Samuel,

assuming your input is something like this:

defmodule User do
  defstruct [:id, :name, :role, :age]
users_list = [
  %User{id: 1, name: "Alice", role: "user", age: 20},
  %User{id: 2, name: "Bob", role: "admin", age: 30},
  %User{id: 3, name: "John", role: "editor", age: 40}

you can do this:, fn user_struct ->
  Map.take(user_struct, [:id, :name])

with this as an output:

[%{id: 1, name: "Alice"}, %{id: 2, name: "Bob"}, %{id: 3, name: "John"}]

or shorter version:, &Map.take(&1, [:id, :name]))

with this as an output:

[%{id: 1, name: "Alice"}, %{id: 2, name: "Bob"}, %{id: 3, name: "John"}]

You will have to adjust that code a bit, based on your needs, of course.

I am not sure, if the keys of the map have to be strings or can be atoms like in my example. If those have to be strings, I would use something like this approach:, fn user_struct ->
  |> Map.take([:id, :name])
  |> {key, value} ->
    {Atom.to_string(key), value}

But I feel like this is an XYproblem

You should be able to provide your structs to your views, and convert them to maps there with only the keys you want. Did you try that approach?


Thanks a lot for your answer Stefan. I was not aware of Map.take/2

What if I wanted to change the name of a key in the return? Let’s say for example, there is a key in the struct named age, but I want to change it in the return for year. Is there a way to do that? Or if I wanted to add a new key with a value from somewhere else?

You’re using That takes a list as an input and returns a list with the same number of elements as output.
Each element of the output is created by the mapper function (the 2nd argument of the

It’s up to me what I will do with that element.

I can ignore the input completely:[:a, :b, :c], fn _ignoring_element -> :x end)
# [:x, :x, :x]`

I can convert each element to a different “type”:[:a, :b, :c], fn element -> Atom.to_string(element) end)
# ["a", "b", "c"]

And in your case, you can do something similar:, fn user_struct ->
    "a" =>,
    "b" =>,
    "c" => DateTime.utc_now(),
    "d" => (1 + 2 * 3)

Thanks again for your answer Stefan.

You perfectly solved my problem.
To be honest I forgot you could use structs with
It’s very easy to use and the code becomes very readable.