experimentix

experimentix

Update map in loop

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"
  }

Marked As Solved

stefanluptak

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.

Also Liked

experimentix

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

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

Enum.reduce(order["products"], order, fn {product_id, _product}, order ->
   update_in(order, ["products", product_id], fn product ->
    Map.put(product, "associated_products", associated_products[product_id])
  end)
end)

Once again thanks

Where Next?

Popular in Questions Top

marius95
Hello everyone, I try to use an Javascript Event Handler in my root.html.leex file. Therefore I created a function in the app.js file: ...
New
Kurisu
For example for a current url like http://localhost:4000/cosmetic/products?_utf8=✓&amp;query=perfume&amp;page=2, I would like to get: ...
New
gshaw
What is the idiomatic way of matching for not nil in Elixir? E.g., First way: defp halt_if_not_signed_in(conn, signed_in_account) when...
New
mgjohns61585
Could someone help me? I’m making my first elixir program, number guessing game. I can’t figure out how to convert the user’s guess from ...
New
johnnyicon
Hi all, I’ve just started learning Elixir and Phoenix Framework, so please pardon my n00bness at this stage. I’m trying to use Postgres...
New
hariharasudhan94
lets say i have a sample like a = 20; b = 10; if (a &gt; b) do {:ok, "a"} end if (a &lt; b) do {:ok, b} end if (a == b) do {:ok, "equa...
New
ycv005
I have followed this StackOverflow post to install the specific version of Erlang. And When I am running mix ecto.setup then getting fol...
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
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
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New

Other popular topics Top

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
lastday4you
I wanted to check elixir version in phoenix because i found that my elixir is 1.5 but when i use Enum.chunk_by it said the function is un...
New
msaraiva
Surface is an experimental library built on top of Phoenix LiveView and its new LiveComponent API that aims to provide a more declarative...
564 43622 214
New
JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New
Lily
In templates/appointment/index.html.eex: &lt;%= for appointment &lt;- @appointments do %&gt; &lt;tr&gt; &lt;td&gt;&lt;%= appoi...
New
joeerl
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
New
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New
dblack
I’ve got an issue with an app and I’ve no idea of how to troubleshoot it. I’m hoping someone here might have seen something similar. I p...
New
boundedvariable
I am going through the kafka architecture. All the features what the kafka is providing are already in Erlang. I would like hear your opi...
New

We're in Beta

About us Mission Statement