tajihiro

tajihiro

How to use Enum.group_by with multi columns

I know how to use Enum.group_by with one column, but I’m not sure how to use it with multi columns.

Following example is group_by(id).

input = %{
  "1" => [
    %{id: "1", seq: "1", lang: "Elixir", name: "Bob"},
    %{id: "1", seq: "1", lang: "Java", name: "Bob"},
    %{id: "1", seq: "2", lang: "Ruby", name: "Bob"}
  ],
  "2" => [
    %{id: "2", seq: "1", lang: "Elixir", name: "Tom"},
    %{id: "2", seq: "2", lang: "C#", name: "Tom"}
  ],
  "3" => [
    %{id: "3", seq: "1", lang: "Python", name: "Dave"},
    %{id: "3", seq: "1", lang: "Java", name: "Dave"},
    %{id: "3", seq: "2", lang: "Ruby", name: "Dave"}
  ],
  "4" => [%{id: "4", seq: "1", lang: "Python", name: "Jane"}]
}

I would like to make group_by with id & seq, in this case.
Is there any idea for it?

Marked As Solved

josevalim

josevalim

Creator of Elixir

You can do: Enum.group_by(collection, &{&1.id, &1.seq})

Also Liked

tajihiro

tajihiro

Thank you so much. It worked as I expected.

Where Next?

Popular in Questions Top

sergio
In Ruby, I can go: User.find_by(email: "foobar@email.com").update(email: "hello@email.com") How can I do something similar in Elixir? ...
New
jerry
Good day to you all. I have been struggling to get a query involving like and ilike to work. Can anyone assist me on this, please? pro...
New
Lily
In templates/appointment/index.html.eex: <%= for appointment <- @appointments do %> <tr> <td><%= appoi...
New
senggen
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] 15:22:35.803 [error] gen_event {lager_file_backend...
New
dokuzbir
I want to highlight html closing tags when i click a html tag. That works in .html files but doesnt work for html.eex templates. How can...
New
beno
I will often find my self writing things similar to: case some_value do nil -> something() "" -> something() _ -> somethi...
New
jason.o
In the code below, if the create action is not set to accept “extra_key” as an input, it errors out with a message shown above. Is there ...
New
dblack
I’ve got an issue with an app and I’ve no idea of how to troubleshoot it. I’m hoping someone here might have seen something similar. I p...
New
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
New
openscript
Hello! Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
New

Other popular topics Top

Darmani72
If I have a post route which an argument: post /my_post_route/:my_param1, MyController.my_post_handler How would get the post params ...
New
sorentwo
Hello! tl;dr Announcing Oban, an Ecto based job processing library with a focus on reliability and historical observability. After spen...
985 42920 311
New
skosch
To my knowledge, put_in, Map.update etc. all have the one limitation of not automatically creating intermediate keys when needed (for exa...
New
JeremM34
Hello, how can I check the Phoenix version ? Thanks !
New
AngeloChecked
What learn first? Rust or Elixir Hi Elixir community! I’m here because i want learn a new language. I’m a junior developer and mainly i ...
New
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New
New
pmjoe
I have a relationship of love and hate with Elixir. Lots of things are just absolutely right, but there are some things that are kind of ...
New
vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
fayddelight
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

We're in Beta

About us Mission Statement