anantharaman445

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

idi527

:waving_hand:

How about

Enum.find(ingredients, fn %{name: name} -> name == "Stuffed Bhindi" end)

Also Liked

mudasobwa

mudasobwa

Creator of Cure

I vote for comprehensions!

for %{name: "Stuffed Bhindi"} = map <- ings, do: map # hd() if there is only one
gerbal

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"]
]


mudasobwa

mudasobwa

Creator of Cure

Indeed!

get_in ingrs, [Access.filter(& &1[:name] == "Stuffed Bhindi"), :ingredients, Access.all(), "name"]

Where Next?

Popular in Questions Top

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
hariharasudhan94
Lets say I have map like this fetching from my database %{"_id" =&gt; #BSON.ObjectId&lt;58eb1a7a9ad169198c3dXXXX&gt;, "email" =&gt; ...
New
Lily
In templates/appointment/index.html.eex: &lt;%= for appointment &lt;- @appointments do %&gt; &lt;tr&gt; &lt;td&gt;&lt;%= appoi...
New
fireproofsocks
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
sergio_101
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
bsollish-terakeet
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
WestKeys
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 Top

minhajuddin
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
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
sen
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
shijith.k
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
dblack
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
JorisKok
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

We're in Beta

About us Mission Statement