formingform

formingform

How to filter a list of maps by multi keys?

Hello!
I’m trying to filter list of maps to remove items where value of key was declared previously in this list. List that I have:

list = [
  %{name: "01", type: "t_01", desc: "desc_01_1"},
  %{name: "02", type: "t_02", desc: "desc_02_1"},
  %{name: "01", type: "t_01", desc: "desc_01_2"},
  %{name: "02", type: "t_02", desc: "desc_02_2"}
]

List what I want to get, it should filter by two keys: name and type

list = [
  %{name: "01", type: "t_01", desc: "desc_01_1"},
  %{name: "02", type: "t_02", desc: "desc_02_1"},
]

Marked As Solved

roflbobl

roflbobl

You can do that with Enum.uniq_by/2 like this

iex> list = [
  %{name: "01", type: "t_01", desc: "desc_01_1"},
  %{name: "02", type: "t_02", desc: "desc_02_1"},
  %{name: "01", type: "t_01", desc: "desc_01_2"},
  %{name: "02", type: "t_02", desc: "desc_02_2"}
]

iex> ist = Enum.uniq_by(list, fn elem -> {elem.name, elem.type} end)

iex> [
  %{name: "01", type: "t_01", desc: "desc_01_1"},
  %{name: "02", type: "t_02", desc: "desc_02_1"},
]

You can read more about the function here: Enum.uniq_by/2

Also Liked

mudasobwa

mudasobwa

Creator of Cure
iex|💧|1 ▸ list |> Enum.group_by(&Map.fetch!(&1, :name)) |> Enum.map(fn {_k, v} -> hd(v) end)
[
  %{name: "01", type: "t_01", desc: "desc_01_1"},
  %{name: "02", type: "t_02", desc: "desc_02_1"}
]

[EDIT] Do not use this. The answer by @roflbobl below is the way to go.

Where Next?

Popular in Questions Top

logicmason
Hi there, I'm working through my first release with elixir/phoenix. I've built a release with distillery and found that it crashes when I...
New
jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod -- where is this set? Thanks.
New
srinivasu
How to handle excepions in elixir? Suppose i have A, B, C ,D, E modules. and each module has get() function. A.get() method will call th...
New
yawaramin
In the Dialyzer docs ( http://erlang.org/doc/man/dialyzer.html#requesting-or-suppressing-warnings-in-source-files ), there is a way to tu...
New
_russellb
I want to try my hand at web scraping. What tools/libraries do I need to use. I’m hoping to turn this into something professional so don’...
New
mgjohns61585
Could someone help me? I'm making my first elixir program, number guessing game. I can't figure out how to convert the user's guess from ...
New
Fl4m3Ph03n1x
Background Let’s assume I have a typical GenServer that receives messages as requests, does some operation in a DB and returns responses....
New
stefanluptak
Hello everybody, usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
New
kostonstyle
Hi all I want to have a unix time, from the current time plus 1 hour. DateTime.now + 1 hour How to get it in elixir? Thanks
New
lucidguppy
I have a super simple question about elixir - how would I take a file like this foo bar baz and output a new file that enumerates th...
New

Other popular topics Top

albydarned
Hello all! I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
New
sergio
I couldn’t find any guides that worked well with Phoenix 1.6.0 and esbuild. I hope this helps people test the waters and eases you into t...
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
malloryerik
Hi, this is for people who, like me, have had some friction using .html.heex templates in VSCode. The solution seems to be, in a hyphena...
New
KronicDeth
Elixir plugin for JetBrain’s IntelliJ Platform (including Rubymine) This is a plugin that adds support for Elixir to JetBrains IntelliJ...
289 35421 110
New
chensan
I have a User schema with a :from_id field set to type :string: defmodule TweetBot.Repo.Migrations.CreateUsers do use Ecto.Migration ...
New
qwerescape
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
shahryarjb
Hello, I have map which I want to convert it to string like this: the map: %{last_name: "tavakkoli", name: "shahryar"} the string I ne...
New
electic
Hi, I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New
magnetic
Hey :wave:t3: Elixir community, I’ve been learning Elixir, and working on some side projects. My editor of choice is VSCode, and althoug...
New

We're in Beta

About us Mission Statement