how do I transform this
[
%{id: "12321", name: "Name1"},
%{id: "231412", name: "Name2"},
%{id: "2131245", name: "Name3"}
]
for
[
{"12321","Name1"},
{"231412","Name2"} ,
{"2131245","Name3"}
]
Has as?
how do I transform this
[
%{id: "12321", name: "Name1"},
%{id: "231412", name: "Name2"},
%{id: "2131245", name: "Name3"}
]
for
[
{"12321","Name1"},
{"231412","Name2"} ,
{"2131245","Name3"}
]
Has as?
The latest is not valid json.
It’s just an Enum.map with an anonymous function taking a map as input, and returning a tuple.
When I run this
cols = [
%{id: "12321", name: "Name1"},
%{id: "231412", name: "Name2"},
%{id: "2131245", name: "Name3"}
]
cols |> List.to_tuple
the result and that
{
%{id: "12321", name: "Name1"},
%{id: "231412", name: "Name2"},
%{id: "2131245", name: "Name3"}
}
Basically now, I just needed to remove these “id:”, “name:”
How can I do this?
Some hint
& {&1.id, &1.name}
I asked the question because I wanted this function to work.
Enum.map(cols, fn {col,_} -> {col,"true"} end) |> Enum.into(%{})
But this is impossible, because You iterate over maps, not tuples.
That’s the problem.
Would you have any suggestions for this to work?
Remembering that the cols result cannot change.
I thought I gave You the solution already.
Sorry, I didn’t notice.
Enum.map (cols, fn% {id: id, name: name} -> {id, name} end)
Thank you
If you want a map as the end result, I’d recommend Map.new/2
Map.new(cols, fn %{id: id, name: name} -> {id, name} end)
Any code of the form Enum.map(input, some_fun) |> Enum.into(%{})
can be expressed as Map.new(input, some_fun)