david234

david234

Elixir update a particular field in a list of maps

I have this list which has order and order_details. I want to update the quantity field inside order details by subtracting it with the quantity_kot field.

[
  %Od.Orders.Order{
    __meta__: #Ecto.Schema.Metadata<:loaded, "orders">,
    created_at: ~N[2022-02-08 07:05:35],
    email: test@gmail.com,
    name: Test,
    order_details: [
      %Od.Orders.OrderDetails{
        __meta__: #Ecto.Schema.Metadata<:loaded, "order_details">,
        created_at: ~N[2022-02-08 07:05:35],
        discount: 0.0,
        id: "7598503c-88ad-11ec-88f0-027d853babb4",
        price: 10.0,
        quantity: 4.0,
        quantity_kot: 0,
      },
      %Od.Orders.OrderDetails{
        __meta__: #Ecto.Schema.Metadata<:loaded, "order_details">,
        created_at: ~N[2022-02-08 07:05:35],
        discount: 0.0,
        id: "7b9e6d2c-88ad-11ec-83e2-027d853babb4",
        price: 10.0,
        quantity: 3.0,
        quantity_kot: 0,
      }
    ],
    overall_discount: 10.5,
    payment_status: "unpaid",
    updated_at: ~N[2022-02-08 07:05:35],
  }
]

I tried to achieve this by using the below code

[updated_lists] =
  for list <- lists do
    for x <- list.order_details do
        x
        |> Map.update!(:quantity, &(&1 - x.quantity_kot))
    end
  end

updated_lists

These updates but the problem is I am getting only the order details from this result. All the fields in order struct is missing.

I want the whole list back with only the quantity field inside the order details to be updated. Any insight on how to achieve this will be greatly appreciated. Thanks.

Marked As Solved

joey_the_snake

joey_the_snake

your inner iteration is returning only the updated list of order_detail maps. it must return the updated order map

for order <- orders do
    updated_order_details = 
        for order_detail <- order.order_detail do
          Map.update!(order_detail, :quantity, &(&1 - order_detail.quantity_kot))
        end

    %{order | order_details: updated_order_details}
end

Where Next?

Popular in Questions Top

belgoros
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
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
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
shahryarjb
Hello, I get Persian date from my client and convert it to normal calendar like this: def jalali_string_to_miladi_english_number(persi...
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
jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod -- where is this set? Thanks.
New
gazoon
I want to know absolute current module path. In python i could do that: os.path.abspath(__file__) Does elixir have anything similar?
New
vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
mcarvalho
What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.
New
kostonstyle
Hi all I want to have a unix time, from the current time plus 1 hour. DateTime.now + 1 hour How to get it in elixir? Thanks
New

Other popular topics Top

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
peerreynders
Manning 2016 Halloween weekend sale via Deal of the Day Friday, October 28 - Half off all MEAPs - code WM102816LT Saturday, October 29 ...
326 29600 154
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
Tee
can someone please explain to me how Enum.reduce works with maps
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
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
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
chrismccord
This release brings a number of exciting features, including integration with the new Phoenix LiveDashboard and Phoenix LiveView. There h...
New
fireproofsocks
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? https://hexdocs.pm/ecto/Ecto.Repo.h...
New
siddhant3030
Hi, I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
New

We're in Beta

About us Mission Statement