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
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
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
Lets say I have map like this fetching from my database
%{"_id" => #BSON.ObjectId<58eb1a7a9ad169198c3dXXXX>, "email" => ...
New
In templates/appointment/index.html.eex:
<%= for appointment <- @appointments do %>
<tr>
<td><%= appoi...
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
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
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
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New
Other popular topics
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
New
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
Hi All,
I set a environment variables in dev.exs , like below code.
when i start server, how can i set the ${enable} value?
thanks.
d...
New
I am trying to start a new phoenix project with elixir 1.9, but mix phx.new does not work. It says that ** (Mix) The task "phx.new" could...
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
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
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
- #elixirconf-us
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #metaprogramming
- #security
- #hex









