Samuel-88
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
|> Map.new(fn user ->
{"#{user.first_name} #{user.last_name}",
%{
"id" => user.id,
"role" => user.role
}}
end)
But what I actually want to return for Phoenix View, is a list with maps like this:
[%{"id" => user.id, "role" => user.role, "name" => user.name}, {"id" => user.id, "role" => user.role, "name" => user.name}, ...]
So this way Phoenix view returns a list of objects like that.
I’m trying to find a solution for this but Map.new() only allows for one key, value pair.
Does anyone know how I could solve this?
Thanks in advance for all the help!
Trending in Questions
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
Hello,
I know there is an approach for handling lists that allows for optimized traversal, but I can’t recall the specific method (somet...
New
Documentation
While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New
So my question is quite simple and i have found no conclusive answer on forum, google or AI.
Should we use :erlang.float for Integer to ...
New
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New
If a change or preparation module uses Ash.Changeset.get_argument/2 or Ash.Query.get_argument/2 (or any of the other get_argument functio...
New
Other Trending Topics
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
I am happy to introduce the very α version of the new programming language compiled to BEAM.
Welcome Cure.
It has literally three kille...
New
Hi there! We created Gust: A task orchestrator inspired by Airflow.
For those who have never heard about Aiflow, it’s a Python-based wor...
New
Hi everyone!
The first release candidate for the Expert language server project is now available!
We’ve published a press release detai...
New
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #library
- #deployment
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixirconf-us
- #ai
- #blog-post
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #metaprogramming
- #hex
- #security










Showing Posts 1 to 4- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
stefanluptak
Hi Samuel,
assuming your input is something like this:
you can do this:
with this as an output:
or shorter version:
with this as an output:
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:
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?
Samuel-88
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?
stefanluptak
You’re using
Enum.map/2. 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
Enum.map).It’s up to me what I will do with that element.
I can ignore the input completely:
I can convert each element to a different “type”:
And in your case, you can do something similar:
Samuel-88
Thanks again for your answer Stefan.
You perfectly solved my problem.
To be honest I forgot you could use structs with Enum.map
It’s very easy to use and the code becomes very readable.