CAIOHOBORGHI

CAIOHOBORGHI

Enum find by id?

How to find index of element inside enum by property?

I have this enum and I need to find the index of an item by its id(or another property).
[%{“code” => “ABEV3”, “comparison” => “>”, “field” => “close”, “id” => “4a9d96ce-e572-11eb-a557-0242ac170003”, “message” => “ABEV3 Close price is higher than 11.9!”, “value” => 11}]

How to do this?

Marked As Solved

Sebb

Sebb

Sorry I most likely confused you with the hint that map-items are given as {k, v} to the function.
You have a list of maps, So you just do:

e = [%{foo: :bar, id: 1}, %{foo: :baz, id: 2}, %{foo: :foobar, id: 3}]
iex(3)> Enum.find_index(e, fn v -> v.id == 1 end)                           
0

So find_index goes through the list element by element, calls the function you provide and if this function return true, the index is returned.

Also Liked

Sebb

Sebb

take a look at the great visual Elixir reference:

https://superruzafa.github.io/visual-elixir-reference/Enum/find_index/2/

hauleth

hauleth

Pinning, so it will match the already bound value instead of rebinding. However in this case I think that it would be more readable to do:

Enum.find(list, fn elem -> elem.id == id end)
entone

entone

you can also use pattern matching

Enum.find_index(list, fn %{id: ^id} -> true; _ -> false end)

you could also use filter to just remove the item

Enum.filter(list, fn %{id: ^id} -> false; _ -> true end)

Where Next?

Popular in Questions Top

Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
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
tduccuong
Hi, is there any work on GUI with Elixir, that is similar to Electron/Javascript? My idea is to bundle Phoenix and BEAM into a single se...
New
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
myronmarston
The Elixir Typespec docs show the following syntax for keyword lists in typespecs: # ... | [key: type] # keyword lis...
New
aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
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
chewm
Hi guys, nice to meet you to the whole forum, I’m new here, I’m trying to configure visual studio code for elixir, right now the intellis...
New
beno
I will often find my self writing things similar to: case some_value do nil -> something() "" -> something() _ -> someth...
New
sabri
Can someone explain the settings of pool_size of Ecto in config file? and what is the recommend size? Thanks
New

Other popular topics Top

shahryarjb
Hello, I get Persian date from my client and convert it to normal calendar like this: def jalali_string_to_miladi_english_number(persi...
New
peerreynders
Manning 2016 Halloween weekend sale via Deal of the Day Friday, October 28 - Half off all MEAPs - code WM102816LT Saturday, October 29 ...
326 29600 154
New
joaquinalcerro
Hi there, I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 record...
New
itssasanka
Hi all, Trying to get some more clarity over utc_datetime and naive_datetime for Ecto: https://hexdocs.pm/ecto/Ecto.Schema.html#module-...
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
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
baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
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
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
Nvim
Elixir appears to be a superior language to Python. I don’t see any advantage of Python over Elixir. Are there any?
New

We're in Beta

About us Mission Statement