germanoclodi
Combine values of a list of Map by a specific field
Hello everyone,
I need to combine a list of Maps into a shorter list of maps, using a specific value as aggregator and key as conditional.
Basically, I need to turn this
[
%{
id: 1,
value: [1]
},
%{
id: 2,
value: [1]
},
%{
id: 2,
value: [3]
},
%{
id: 3,
value: [1]
},
%{
id: 2,
value: [4]
},
%{
id: 3,
value: [2]
}
]
into this
[
%{
id: 1,
value: [1]
},
%{
id: 2,
value: [1, 3, 4]
},
%{
id: 3,
value: [1, 2]
}
]
I’ve achieved this result using Map.merge, passing two specific items of those lists:
Map.merge(a, b, fn x, v1, v2 ->
case x do
:value -> Enum.concat(v1, v2)
_ -> v1
end
end)
But I’m unable to replicate this to a list of Maps.
I’ve imagined using Enum.reduce, or Enum.group_by, but nothing seems to work.
Could anyone help me, please?
Most Liked
gregvaughn
group_by is the easiest way to start, though it could be solved more efficiently with reduce
input
|> Enum.group_by(& &1.id)
|> Enum.map(fn {id, list} -> %{id => Enum.flat_map(list, & &1.value)} end)
edit: The above isn’t quite right
Correction:
input
|> Enum.group_by(& &1.id)
|> Enum.map(fn {id, list} -> %{id: id, value: Enum.flat_map(list, & &1.value)} end)
1
Popular in Questions
I have an umbrella app.
Some of the apps inside depend on other apps in the umbrella, unsurprisingly.
I'm writing a test for one of t...
New
How to set Jason to encode all fields in ecto schema, I don’t care about security and implementing only is taking long list of attributes...
New
I am trying to figure out how Mix knows whether the environment is test, dev, or prod -- where is this set?
Thanks.
New
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
I have a User schema with a :from_id field set to type :string:
defmodule TweetBot.Repo.Migrations.CreateUsers do
use Ecto.Migration
...
New
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? https://hexdocs.pm/ecto/Ecto.Repo.h...
New
Hi all,
I've just started learning Elixir and Phoenix Framework, so please pardon my n00bness at this stage.
I'm trying to use Postg...
New
Hey all,
I discovered Elixir and I love it. I always wanted to learn a functional programming and I intended to go for Haskell, but afte...
New
Can someone explain the settings of pool_size of Ecto in config file? and what is the recommend size?
Thanks
New
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New
Other popular topics
TL;DR: I’ve just released an implementation of Microsoft’s IDE-independent Language Server Protocol for Elixir. It adds language support ...
New
As promised, the first release candidate of Phoenix 1.3.0 is out! This release focuses on code generators with improved project structure...
New
Using vs code and installed ElixirLS: support and debugger.
And I got an error popped up on start up says
Failed to run ‘elixir’ comma...
New
None of the current solutions worked well for me, so I went ahead and built a user management system from scratch.
This project took far...
New
Is there a way to get the call stack or stack trace at any point in the code? Not from exceptions, but an expression that returns how the...
New
I tried installing
elixir 1.11.2
erlang 23.3.4
via asdf in my zsh shell. Enabled the versions locally and globally.
When I list them ...
New
Surface is an experimental library built on top of Phoenix LiveView and its new LiveComponent API that aims to provide a more declarative...
New
Hi all,
I've just started learning Elixir and Phoenix Framework, so please pardon my n00bness at this stage.
I'm trying to use Postg...
New
I am using the Starship cross-shell prompt – it seems pretty nice, but I get some errors:
[WARN] - (starship::utils): Executing command ...
New
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New







