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

fireproofsocks
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
ovidiubadita
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
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
johnnyicon
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
jerry
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
joeerl
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
jason.o
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
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
nsuchy
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
hariharasudhan94
Lets say I have map like this fetching from my database %{"_id" =&gt; #BSON.ObjectId&lt;58eb1a7a9ad169198c3dXXXX&gt;, "email" =&gt; ...
New

Other popular topics Top

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
Darmani72
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
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
joeerl
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
pmjoe
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
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
jason.o
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
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
marick
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
svb
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

We're in Beta

About us Mission Statement