How to obtain %{key: value} with list with nested map

My list looks like this

[ %{
    "handle" => "a",
    "p" => [
      %{
        "price" => "$10.00",
      }
    ],
    "scheme" => "unit",
  },
  %{
    "handle" => "b",
    "p" => [
      %{

        "price" => "$14.95",
      }
    ],
    "scheme" => "unit",
  },
 %{
    "handle" => "c",
    "p" => [
      %{
        "price" => "$22.95",
      }
    ],
    "scheme" => "unit",
  }
]

How can I iterate through the list and get result as below??

[
  %{handle: "a",price: "$10.00"},
  %{handle: "b", price: "$14.95"},
  %{handle: "c", price: "$22.95"},

]
list
|> Enum.flat_map(fn %{"handle" => handle, "p" => prices} ->
  Enum.map(prices, &%{handle: handle, price: &1["price"]})
end)
1 Like

Thank you…

Alternatively you can use a for expression:

for %{"handle" => handle, "p" => prices} <- list,
    %{"price" => price} <- prices do
  %{handle: handle, price: price}
end
1 Like

Thanks…