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
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
4
Also Liked
Popular in Questions
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
I am trying to figure out how Mix knows whether the environment is test, dev, or prod -- where is this set?
Thanks.
New
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
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
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
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
Background
Let’s assume I have a typical GenServer that receives messages as requests, does some operation in a DB and returns responses....
New
Hello everybody,
usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
New
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
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
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
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
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
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
Elixir plugin for JetBrain’s IntelliJ Platform (including Rubymine)
This is a plugin that adds support for Elixir to JetBrains IntelliJ...
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
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
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
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
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







