anantharaman445
List of nested maps
Hi I’ve a list of nested maps as like this
[
%{
ingredients: [
%{"measuring_unit" => "gms", "name" => "okra/bhindi", "qty" => 450},
%{"measuring_unit" => "tbsp", "name" => "vegetable oil", "qty" => "2"},
%{
"measuring_unit" => "tbsp",
"name" => "desiccated coconut powder",
"qty" => 4
},
%{"measuring_unit" => "tbsp", "name" => "coriander powder", "qty" => 2},
%{"measuring_unit" => "tbsp", "name" => "cumin powder", "qty" => 2},
%{"measuring_unit" => "tbsp", "name" => "ground cumin", "qty" => 1.5},
%{"measuring_unit" => "tbsp", "name" => "cinnamon stick", "qty" => 1},
%{
"measuring_unit" => "tbsp",
"name" => "dried mango powder, also known as amchur",
"qty" => 1
},
%{
"measuring_unit" => "tbsp",
"name" => "red pepper flakes",
"qty" => 0.25
},
%{"measuring_unit" => "tbsp", "name" => "red chili powder", "qty" => 1},
%{"measuring_unit" => "tbsp", "name" => "garam masala", "qty" => 1},
%{"measuring_unit" => "tbsp", "name" => "turmeric powder", "qty" => 0.25},
%{"measuring_unit" => "tbsp", "name" => "salt, or to taste", "qty" => 2},
%{
"measuring_unit" => "tbsp",
"name" => "vegetable oil, divided",
"qty" => 2
}
],
name: "Stuffed Bhindi",
other_ingredients: []
},
%{
ingredients: [
%{"measuring_unit" => "gms", "name" => "okra/bhindi", "qty" => 450},
%{"measuring_unit" => "tbsp", "name" => "vegetable oil", "qty" => "2"},
%{
"measuring_unit" => "tbsp",
"name" => "desiccated coconut powder",
"qty" => 4
},
%{"measuring_unit" => "tbsp", "name" => "coriander powder", "qty" => 2},
%{"measuring_unit" => "tbsp", "name" => "cumin powder", "qty" => 2},
%{"measuring_unit" => "tbsp", "name" => "ground cumin", "qty" => 1.5},
%{"measuring_unit" => "tbsp", "name" => "cinnamon stick", "qty" => 1},
%{
"measuring_unit" => "tbsp",
"name" => "dried mango powder, also known as amchur",
"qty" => 1
},
%{
"measuring_unit" => "tbsp",
"name" => "red pepper flakes",
"qty" => 0.25
},
%{"measuring_unit" => "tbsp", "name" => "red chili powder", "qty" => 1},
%{"measuring_unit" => "tbsp", "name" => "garam masala", "qty" => 1},
%{"measuring_unit" => "tbsp", "name" => "turmeric powder", "qty" => 0.25},
%{"measuring_unit" => "tbsp", "name" => "salt, or to taste", "qty" => 2},
%{
"measuring_unit" => "tbsp",
"name" => "vegetable oil, divided",
"qty" => 2
}
],
name: "Normal Bhindi",
other_ingredients: []
}
]
I just wanted to extract the data where name = “Stuffed Bhindhi”
I tried this ingre_map = Enum.take_while(ingredientslist, fn x → ingredientslist[:name] == “Stuffed Bhindhi”)
but didn’t work getting nil
Marked As Solved
idi527
![]()
How about
Enum.find(ingredients, fn %{name: name} -> name == "Stuffed Bhindi" end)
4
Also Liked
mudasobwa
Creator of Cure
I vote for comprehensions!
for %{name: "Stuffed Bhindi"} = map <- ings, do: map # hd() if there is only one
4
gerbal
In a pipeline if all instances implement Access
ingredients
|> Enum.filter(& &1[:name] == "Stuffed Bhindi")
|> Enum.map(& get_in(&1, [:ingredients, Access.all(), "name"]))
[
["okra/bhindi", "vegetable oil", "desiccated coconut powder",
"coriander powder", "cumin powder", "ground cumin", "cinnamon stick",
"dried mango powder, also known as amchur", "red pepper flakes",
"red chili powder", "garam masala", "turmeric powder", "salt, or to taste",
"vegetable oil, divided"]
]
1
mudasobwa
Creator of Cure
Indeed!
get_in ingrs, [Access.filter(& &1[:name] == "Stuffed Bhindi"), :ingredients, Access.all(), "name"]
1
Popular in Questions
I’m working on defining a simple Ecto schema for a table (in PostGres), but I don’t see where I can define a column as NOT NULL. Conside...
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
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
Hi all,
I’ve just started learning Elixir and Phoenix Framework, so please pardon my n00bness at this stage.
I’m trying to use Postgres...
New
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
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
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
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
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New
Lets say I have map like this fetching from my database
%{"_id" => #BSON.ObjectId<58eb1a7a9ad169198c3dXXXX>, "email" => ...
New
Other popular topics
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible.
total = 10
while total != 0
...
New
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
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? Ecto.Repo — Ecto v3.14.0 has exampl...
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
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
Credo is smart enough to check for (something like) this:
assert length(the_list) == 0
with this response:
Checking if an enum is empt...
New
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
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
I had some trouble figuring out how to make many-to-many associations work. Once I got it working, I wrote a blog post. Because I’m a nov...
New
Hi!
Currently I want to submit a form by pressing the Enter key. However, since my input field is of type “textarea” this is just adds a...
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #supervisor
- #advent-of-code
- #elixirconf-us
- #distillery
- #processes
- #forms
- #api
- #metaprogramming
- #security
- #performance








