experimentix
Hello
I’m new to elixir and i’m struggling with this problem
I’ve a map lets call it order and that has several products
I want to loop through this products map inside the order map and append some info from another map called associated products
it looks like this
orders map
%{
"_csrf_token" => "gwsgw",
"custom_tags" => "",
"id" => "62212",
"products" => %{
"123456" => %{
"category" => "3",
"tag" => ["hero", "superstar"],
"title" => "Product title"
},
"234567" => %{"category" => "5", "title" => "Product title"},
"345678" => %{
"category" => "8",
"tag" => ["creation", "dynasty"],
"title" => "Product title"
},
"456789" => %{
"category" => "5",
"title" => "Product title"
},
},
"order_id" => "193885",
"custom_message" => "Nothing to declare",
"tags_title" => %{"tag" => ["preview"]},
"allcats" => "5"
}
associated products map
%{
"123456" => [
%{
"id" => "4984944",
"has_stock" => false,
"name" => "Other product Name",
"product_id" => "123456",
},
%{
"id" => "3511064",
"has_stock" => true,
"name" => "Other product Name",
"product_id" => "123456",
}
],
"234567" => [
%{
"id" => "3511075",
"has_stock" => true,
"name" => "Other product Name",
"product_id" => "234567",
},
%{
"id" => "3511076",
"has_stock" => false,
"name" => "Other product Name",
"product_id" => "234567",
}
],
"345678" => [
%{
"id" => "3511082",
"has_stock" => true,
"name" => "Other product Name",
"product_id" => "345678",
},
%{
"id" => "3511083",
"has_stock" => false,
"name" => "Other product Name",
"product_id" => "345678",
}
]
}
i tried this code to put the associated products but it only gets updated with the last item, i assume its because it doesn’t update the order in every loop
order = for {pid, v} <- order["products"] do
IO.inspect pid
IO.inspect v
order = put_in(order, ["products", pid,"associated_products"], associated_products[pid])
end
How can i achieve this result??
%{
"_csrf_token" => "gwsgw",
"custom_tags" => "",
"id" => "62212",
"products" => %{
"123456" => %{
"category" => "3",
"tag" => ["hero", "superstar"],
"title" => "Product title",
"associated_products" => [
%{
"id" => "4984944",
"has_stock" => false,
"name" => "Other product Name",
"product_id" => "123456",
},
%{
"id" => "3511064",
"has_stock" => true,
"name" => "Other product Name",
"product_id" => "123456",
}
]
},
"234567" => %{"category" => "5",
"title" => "Product title",
"associated_products" => [
%{
"id" => "4984944",
"has_stock" => false,
"name" => "Other product Name",
"product_id" => "234567",
},
%{
"id" => "3511064",
"has_stock" => true,
"name" => "Other product Name",
"product_id" => "234567",
}
]
},
"345678" => %{
"category" => "8",
"tag" => ["creation", "dynasty"],
"title" => "Product title",
"associated_products" => [
%{
"id" => "4984944",
"has_stock" => false,
"name" => "Other product Name",
"product_id" => "345678",
},
%{
"id" => "3511064",
"has_stock" => true,
"name" => "Other product Name",
"product_id" => "345678",
}
]
},
"456789" => %{
"category" => "5",
"title" => "Product title",
"associated_products" => [
%{
"id" => "4984944",
"has_stock" => false,
"name" => "Other product Name",
"product_id" => "456789",
},
%{
"id" => "3511064",
"has_stock" => true,
"name" => "Other product Name",
"product_id" => "456789",
}
]
},
},
"order_id" => "193885",
"custom_message" => "Nothing to declare",
"tags_title" => %{"tag" => ["preview"]},
"allcats" => "5"
}
Trending in Questions
Hello!
Suppose you are building workflow (order / task / payment) processing system with the following requirements:
Each workflow con...
New
I’m in search of an Elixir library that offers PDF generation capabilities similar to Ruby’s Prawn. While there have been discussions abo...
New
I’m looking to build a personal workflow to quickly deploy web applications written in elixir/phoenix, for local consumption (ie not on t...
New
Hey guys,
I’ve got a huge CSV ( around 10 GB ) that needs to be processed hourly
Do you guys have any suggestions what is the best prac...
New
Before I dive in myself, did anyone successfully sprinkle Hologram into their existing LiveView app?
Looking for hints regarding:
Addi...
New
Kia ora,
We have been using elixir-google-api to connect to Google Drive. However, with the updates to Tesla due to CVEs this is now bro...
New
Hi all, I wanted to ask how the community is dealing with post-release steps.
Today we have Ecto migrations, which make sure that the db...
New
Other Trending Topics
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge & Solve.
They are GUI (Emerge) and State management (S...
New
Emily is an Elixir library that runs Nx computations on Apple’s MLX. Install it as the default Nx backend and Nx, defn, Axon, Nx.Serving,...
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #blog-post
- #phoenix_html
- #iex
- #ai
- #graphql
- #genstage
- #elixirconf-us
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #metaprogramming
- #security
- #hex










Showing Posts 1 to 5- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
stefanluptak
Check this gist: update_map_in_loop.md · GitHub
It’s a Livebook, so you can copy and paste it into your Livebook server and play with it.
If you need more explanation, I can provide it. I am pretty sure there are more elegant ways to achieve this, but this should be good enough as a start.
The problem with your solution is that it’s not “reducing” the order (keeping the changes for all iterations), just returning the list of modified orders. Each order (item of the returned list) is the order modified but just for a single product.
Hope this helps a bit.
experimentix
Thank you very much, it worked. And thanks for small explanation.
I think i understand how things work now looking at your code.
Once again thanks
experimentix
Since my associated products has the key same as the product key i ended with removing some of your code and referencing it like this
Once again thanks
ConstantBall
This will give a value of
nilto"associated_products"if theproduct_iddoesn’t exist in theassociated_productsmap. Is that the behaviour you want? Are you looking to set another default value if that’s the case? Or do you only want to insert"associated_products"intoorder["products"][product_id]ifassociated_products[product_id]exists?experimentix
That is the behavior i want as associated product is always present.