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

:wave:

How about

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

This one worked man!! Thanks :smile:

I vote for comprehensions!

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

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 Like

Indeed!

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