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

:wave:

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

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
aalberti333
As the title describes, I’m trying to run Enum.map() over a list of key/value pairs, where the value is a map. My data looks like this: ...
New
quazar
How to set Jason to encode all fields in ecto schema, I don’t care about security and implementing only is taking long list of attributes...
New
vac
Hi, I'm quite new in Elixir and I'm trying to format a string to a PEM format. I have the certificate value like MIIDBTCCAe2...... and ...
New
freewebwithme
Using vs code and installed ElixirLS: support and debugger. And I got an error popped up on start up says Failed to run ‘elixir’ comma...
New
myronmarston
The Elixir Typespec docs show the following syntax for keyword lists in typespecs: # ... | [key: type] # keyword lis...
New
Fl4m3Ph03n1x
Background Let’s assume I have a typical GenServer that receives messages as requests, does some operation in a DB and returns responses....
New
shahryarjb
Hello, I have map which I want to convert it to string like this: the map: %{last_name: "tavakkoli", name: "shahryar"} the string I ne...
New
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers' Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New
vrod
I am using the Starship cross-shell prompt – it seems pretty nice, but I get some errors: [WARN] - (starship::utils): Executing command ...
New

Other popular topics Top

sergio
In Ruby, I can go: User.find_by(email: "foobar@email.com").update(email: "hello@email.com") How can I do something similar in Elixir? ...
New
lessless
I believe there are people here who are dealing with CSV files import on the daily basis, and since Excel is a really popular tool there ...
New
AstonJ
You’re a programmer, so you don’t need spoon feeding with the conventional drivel about “this is an integer.” No. You need to know what’s...
New
KronicDeth
Elixir plugin for JetBrain’s IntelliJ Platform (including Rubymine) This is a plugin that adds support for Elixir to JetBrains IntelliJ...
289 35421 110
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
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
freewebwithme
Using vs code and installed ElixirLS: support and debugger. And I got an error popped up on start up says Failed to run ‘elixir’ comma...
New
stefanluptak
Hello everybody, usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
New
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID&lt;0.412.0&gt; terminating ** (Postgrex.Error) FATAL...
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

We're in Beta

About us Mission Statement